Jump to content

Why wrap mySQL in "if" statement?


ChenXiu

Recommended Posts

I do this:
$result = $mysqli -> query("SELECT * FROM mytable") {
// do something
}

Many online tutorials wrap with "if"
if (  $result = $mysqli -> query("SELECT * FROM mytable")  ) {
// do something
}

After trying it both ways with hundreds of different variations, I cannot produce differing results...
What POSSIBLY could be gained by wrapping my mysql queries with an "if" statement?

 

Link to comment
Share on other sites

1 hour ago, ChenXiu said:

I do this:
$result = $mysqli -> query("SELECT * FROM mytable") {
// do something
}

in what php version does that not produce a syntax error? is that your EXACT code?

if that was in fact, like this -

$result = $mysqli -> query("SELECT * FROM mytable"); {
// do something
}

php allows extra {} to exist that are not part of any actual statement, but they have no significance. the code inside of them is always executed. so, for the case of a successful query() statement, it would appear to you that doing this and using an if(){} are the same.

however, as @Barand has posted, using an if(){} conditional around the query() code insures that the // do something code is only executed if the query() was successful. the problem with this is that nothing is done for the case where the query failed and in most cases a failed query is a fatal problem, due to a programming mistake. in this case, when learning, developing, and debugging, you would like the raw database error information to be displayed, so that you have immediate feedback as to if and why the query statement failed (when running this code on a live/public server, you would like this information to be logged instead.) the simple way of accomplishing this, without adding even more code at each database statement that can fail or editing/removing code when moving between development and a live server, is to use exceptions for database statement errors and in most cases let php catch and handle the exception, where php will use its error related settings to control what happens with the actual error information (database statement errors will 'automatically' get displayed/logged the same as php errors.) you can then remove any existing error handling logic for database statements as it will no longer get executed upon an error (execution transfers to the nearest correct type of exception handling upon an error.) your main code only has to deal with error free database statement execution, simplifying the code. if you want to do this, someone can post the statement that will set the mysqli error mode to use exceptions. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.