Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Does it have to die</p> </blockquote> <p>Quite contrary, <strong>it shouldn't <code>die()</code> ever.</strong><br> PHP is a language of bad heredity. Very bad heredity. And <code>or die()</code> with error message being one of the worst rudiments:</p> <ul> <li>die throws an error message out, revealing some system internals to a potential attacker</li> <li>it is confusing innocent users with strange messages and leaving them no interface to work with, so they'd likely just drop out.</li> <li>it kills the script in the middle, so it may cause torn design (or no design at all) shown (i.e. an incomplete render of the page the user requested)</li> <li>killing the script irrecoverably. While thrown exception can be caught and gracefully handled</li> <li><code>die()</code> gives you no hint of the <strong>place where error occurred</strong>. And in a relatively big application it will be quite a pain to find.</li> </ul> <p>So, never use <code>die()</code> with mysql errors, <strong>even for the temporary debugging</strong>: there are better ways. </p> <p>For your query you have but 2 choices:</p> <ul> <li><p>if you are going to use <code>mysqli_query()</code> all the way in your application code (which is wrong but on StackOverflow you will never be taught any other way), you can use <code>trigger_error()</code> instead of die. It will raise a conventional PHP error and will be logged automatically, depending on PHP settings.</p> <pre><code>$result = mysqli_query($link , $sql) or trigger_error($link-&gt;error."[ $sql]"); </code></pre></li> <li><p>if you are going to use <code>mysqli_query()</code> as a part of abstraction library, you have to throw a new Exception instead, because you will need some stack trace (which always supplied with exception) to have an idea of where this error occurred. </p></li> </ul> <p>However, you cannot use <code>new Exception</code> with the <code>OR</code> operator. So, the code becomes a little longer:</p> <pre><code>$result = mysqli_query($link , $sql); if (!$result) { throw new Exception(mysqli_error($link)."[ $sql]"); } </code></pre> <p>Which is not a big deal as you will have to write it only once.</p> <p><strong>Update.</strong> As it turned out, <a href="https://stackoverflow.com/a/18457822/285587">mysqli is able to throw exceptions by itself</a>, which can relieve us from writing handling code manually: </p> <pre><code>$result = mysqli_query($link, $sql); </code></pre> <p>This code will throw an exception in case of error and thus you will always be informed without extra code. However, in previous examples we have an SQL query added to the error message, which can be quite valuable - so, one could stick to the aforementioned methods as well.</p> <p>One important note</p> <blockquote> <p>a predetermined function that writes a log of the error to another table?</p> </blockquote> <p>This is apparently a <strong>bad idea</strong>. Especially if you want to write an error message to the same medium which failed previous attempt.<br> Errors have to be logged into most robust medium - a plain text log. So, just set up your PHP to write error logs and check them regularly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload