mysql_error() on its own doesn't really tell you very much at all. I have written quite a large system with many files, most of them having several queries. All my queries are of the form:
Code: Select all
$rs = mysql_query($query) or db_error(__FILE__, __LINE__, $query, "any extra info");
Code: Select all
<?php
// Copyright © 2008, J. R. Goodenough
function db_error($file, $line, $query, $extra)
{
$_SESSION['error_file'] = basename($file);
$_SESSION['error_line'] = $line;
$_SESSION['error_extra'] = empty($extra) ? "(none)" : $extra;
$_SESSION['error_number'] = mysql_errno();
$_SESSION['error_desc'] = mysql_error();
$_SESSION['error_query'] = $query;
echo "<script type='text/javascript'>
document.location.href='error.php?".SID."'</script>";
exit();
}
?>
99 times out of 100 I have been able to see what's wrong immediately (such as missing substitutions in the query) and, most importantly, where it happened.
Hope this helps someone.