Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote this twice before in the last two days (so for me it's a duplicate even though the questions started a bit different).</p> <p>Each method of mysqli can fail. You should test each return value. If one fails, think about whether it makes sense to continue with an object that is not in the state you expect it to be. (Potentially not in a "safe" state, but I think that's not an issue here.)</p> <p>Since only the error message for the last operation is stored per connection/statement you might lose information about <em>what</em> caused the error if you continue after something went wrong. You might want to use that information to let the script decide whether to try again (only a temporary issue), change something or to bail out completely (and report a bug). And it makes debugging a lot easier.</p> <pre><code>$stmt = $mysqli-&gt;prepare("INSERT INTO testtable VALUES (?,?,?)"); // prepare() can fail because of syntax errors, missing privileges, .... if ( false===$stmt ) { // and since all the following operations need a valid/ready statement object // it doesn't make sense to go on // you might want to use a more sophisticated mechanism than die() // but's it's only an example die('prepare() failed: ' . htmlspecialchars($mysqli-&gt;error)); } $rc = $stmt-&gt;bind_param('iii', $x, $y, $z); // bind_param() can fail because the number of parameter doesn't match the placeholders in the statement // or there's a type conflict(?), or .... if ( false===$rc ) { // again execute() is useless if you can't bind the parameters. Bail out somehow. die('bind_param() failed: ' . htmlspecialchars($stmt-&gt;error)); } $rc = $stmt-&gt;execute(); // execute() can fail for various reasons. And may it be as stupid as someone tripping over the network cable // 2006 "server gone away" is always an option if ( false===$rc ) { die('execute() failed: ' . htmlspecialchars($stmt-&gt;error)); } $stmt-&gt;close(); </code></pre> <p>edit: just a few notes six years later....<br> The mysqli extension is perfectly capable of reporting operations that result in an (mysqli) error code other than 0 via exceptions, see <a href="http://docs.php.net/manual/en/mysqli-driver.report-mode.php" rel="noreferrer">mysqli_driver::$report_mode</a>.<br> <a href="http://docs.php.net/die" rel="noreferrer">die()</a> is really, really crude and I wouldn't use it even for examples like this one anymore.<br> So please, only take away the fact that <em>each and every</em> (mysql) operation <em>can</em> fail for a number of reasons; even <em>if</em> the exact same thing went well a thousand times before....</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