Mysql Query inside a php variable
At my workplace, we were having problems with a certain field. From time to time we need to suspend someone from a mailing list, and to do that, we would just update their record to make the suspend field = Y.
That works no problem in phpMyAdmin, but when we use the crud pages for the staff, sometimes it fails to update, leaving the value of Suspend = N. After looking at the code, I wanted to know if the following line could be the source of the problem.
$rs = mysql_query($sql, $conn) or die("Query has Failed : $sql");
Everything else before it looks good, and it is the last line in the script. Now, I would think that this shouldn't work, but it does. This will run the query. I would think that it would only work if it was
mysql_query($sql, $conn) or die("Query has Failed : $sql");
But it seems to work fine on mo开发者_StackOverflow中文版st occasions. Only every now and then it doesn't work. Could this be the cause of the problem? One last bit of information, we are using MyIsam for the engine.
I would appreciate any help you could give!
mysql_query will return a value whether you're assigning that return to a variable or not. By PHP's operator precedence rules, the first statement is seen as:
$rs = (
(mysql_query($sql, $conn))
or
(die("Query has Failed..."))
);
What's the query look like? Remember that mysql_query can return a "success" status, even though the query has failed to do what you intended. e.g. UPDATE ... SET ... WHERE (somefield = value_that_doesnt_exist);
. The query didn't do what you wanted, but it also wasn't invalid, so mysql_query will not return FALSE and won't trigger the or die(...)
.
精彩评论