Have you thought about using the PEAR DB libary? then you can use the DB::isError to detect the errors and print out the backtrace which will give yuo the line number and query beign called and a wholoe load of ther information.
It will also make your code more portable, as if you change database, say from mysql to mssql or postgres etc, you just need to change the DSN and the rest of your code stays the same.
If you have command line access you can install (go-pear) then use the commands
pear install db to install the DB libary (if its not bundled its been a while since i've installed a new server).
If you don't yuo will need to download the main PEAR libaries and DB.
Then to use it, its simply a matter of
require_once('PEAR.php');
require_once('DB.php');
// define the DSN, usually you'd do this in a global include file so you have a single place to change it. in this example I'm using the mysqli extension form php5
$dsn = "mysqli://username:password@host/database";
// connect and check for errors
if ( DB::isError( $db = &DB::connect( $dsn ) ) ) {
//die printing the reason for the error
die($db->getMessage());
}
// Set the fetch mode to be an associate array, ie heading=>value
$db->setFetchMode( DB_FETCHMODE_ASSOC );
// create a query
$query = "SELECT * FROM table;";
// run the query whilst checking for errors
if( DB::isError( $rtn = $db->getAll( $query ) ) {
// Die printing trn which is now an error object for more information
die( $rtn);
}
if an error occurs, PEAR DB returns an erorr object. Calling the getMessage() method will return the SQL error, or you could print_r() the var (see the end of my code where I do a die( $rtn ); which will print the error object which will show you a lot more info