ChenXiu Posted May 29, 2021 Share Posted May 29, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/312808-why-wrap-mysql-in-if-statement/ Share on other sites More sharing options...
Barand Posted May 29, 2021 Share Posted May 29, 2021 To check if the query failed or not. If query() fails it returns false. Quote Link to comment https://forums.phpfreaks.com/topic/312808-why-wrap-mysql-in-if-statement/#findComment-1586888 Share on other sites More sharing options...
mac_gyver Posted May 29, 2021 Share Posted May 29, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312808-why-wrap-mysql-in-if-statement/#findComment-1586889 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.