Elaborating on yasaluyari
's answer I would stick with something like this:
We can just modify our mysql_query as follows:
function mysql_catchquery[$query,$emsg='Error submitting the query']{
if [$result=mysql_query[$query]] return $result;
else throw new Exception[$emsg];
}
Now we can simply use it like this, some good example:
try {
mysql_catchquery['CREATE TEMPORARY TABLE a [ID int[6]]'];
mysql_catchquery['insert into a values[666],[418],[93]'];
mysql_catchquery['insert into b[ID, name] select a.ID, c.name from a join c on a.ID=c.ID'];
$result=mysql_catchquery['select * from d where ID=7777777'];
while [$tmp=mysql_fetch_assoc[$result]] { ... }
} catch [Exception $e] {
echo $e->getMessage[];
}
Note how beautiful it is. Whenever any of the qq fails we gtfo with our errors. And you can also note that we don't need now to store the state of the writing queries into a $result
variable for verification, because our function now handles it by itself. And the same
way it handles the selects, it just assigns the result to a variable as does the normal function, yet handles the errors within itself.
Also note, we don't need to show the actual errors since they bear huge security risk, especially so with this outdated extension. That is why our default will be just fine most of the time. Yet, if we do want to notify the user for some particular query error, we can always pass the second parameter to display our custom error message.
[PHP 4, PHP 5]
mysql_error — Returns the text of the error message from previous MySQL operation
Description
mysql_error[resource $link_identifier
= NULL]: string
Parameters
link_identifier
The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect[] is assumed. If no such link is found, it will try to create one as if mysql_connect[] had been called with no arguments. If no connection is found or established, an
E_WARNING
level error is generated.
Return Values
Returns the error text from the last MySQL function, or ''
[empty string] if no error occurred.
Examples
Example #1 mysql_error[] example
The above example will output something similar to:
1049: Unknown database 'nonexistentdb' 1146: Table 'kossu.nonexistenttable' doesn't exist
See Also
- mysql_errno[] - Returns the numerical value of the error message from previous MySQL operation
- » MySQL error codes
aleczapka _at] gmx dot net ¶
18 years ago
If you want to display errors like "Access denied...", when mysql_error[] returns "" and mysql_errno[] returns 0, use $php_errormsg. This Warning will be stored there. You need to have track_errors set to true in your php.ini.
Note. There is a bug in either documentation about error_reporting[] or in mysql_error[] function cause manual for mysql_error[], says: "Errors coming back from the MySQL database backend no longer issue warnings." Which is not true.
Florian Sidler ¶
12 years ago
Be aware that if you are using multiple MySQL connections you MUST support the link identifier to the mysql_error[] function. Otherwise your error message will be blank.
Just spent a good 30 minutes trying to figure out why i didn't see my SQL errors.
Pendragon Castle ¶
13 years ago
Using a manipulation of josh >'s function, I created the following. It's purpose is to use the DB to store errors. It handles both original query, as well as the error log. Included Larry Ullman's escape_data[] as well since I use it in q[].