To make my life a bit easier, I created a function DoQuery:
// DoQuery
function DoQuery($sql)
{
global $cm_db_host,$cm_db_user,$cm_db_pass,$cm_db_name;
$con = mysql_connect($cm_db_host,$cm_db_user,$cm_db_pass);
if(!$con) return 1001;
$db = mysql_select_db($cm_db_name);
if(!$db) return 1002;
$res = mysql_query($sql);
if(!$res) return 1003;
return $res;
mysql_close();
}
The function works perfectly – it seems..
Here is how I use it:
$res = DoQuery("UPDATE table SET column1='value 1',column2='value 2' WHERE id=1;");
switch($res){
case true:
echo 'Response[success]ENDCMTAG';
break;
case 1001:
die('Response[dberror1001]ENDCMTAG\r\n'.
'MySQLError['.mysql_error().']ENDCMTAG\r\n');
break;
case 1002:
die('Response[dberror1002]ENDCMTAG\r\n'.
'MySQLError['.mysql_error().']ENDCMTAG\r\n');
break;
case 1003:
die('Response[dberror1003]ENDCMTAG\r\n'.
'MySQLError['.mysql_error().']ENDCMTAG\r\n');
break;
}
Now, when I purposely screw up the SQL statement, so case 1003 should be used, the case true is what is being executed.
According to the PHP manual (yes, I did look it up! 😉 ) a MySQL INSERT, UPDATE …. will either return true or false. In my DoQuery function, whenever a MySQL function returns false, I output specific error codes instead, so I know where it went wrong. If no error occurs, $res should be true, and then the case true should be executed, right?
The problem is that it always outputs true, even if I change the SQL query to rubbish.
If I put the case true at the bottom of the switch, the case 1001 is always executed when everything goes OK – I know because I check the mysql_error() field, and its empty.
What am I doing wrong?
You are returning the value
1003, which evaluates to a “truthy” value when compared against a boolean. It will always evaluate toTRUE.You cannot use a strict comparison
===either, since in the true case, you are returning a MySQL result resource, not the booleanTRUE.If you rearrange your
switch()so that the valid resource case is at the bottom, your error cases will be correctly evaluated first.However, this is a little weird and could cause problems if your function ever returns a different error value (if it is changed,) but the switch isn’t updated. Instead, you should check for a true/false return on the value first and then pass it to the switch to evaluate error codes: