1

How do i get a detailed error description during a php-mysql script run?

I have the following statements where the script fails and displays the custom error message - the contents of the "or die".

I want to get the actual error from MySQL (instead of the custom error i have mentioned), which would give better idea about the scenario - whether its a database issue or server connectivity issue etc..

this is the code i have where i need to enhance the error reporting

$query = "SELECT * FROM table_name";
$result = mysqli_query($db_conn, $query) 
  or die('Connected to database, but querying failed');

thanks!

chiborg
  • 26,978
  • 14
  • 97
  • 115
arun nair
  • 3,643
  • 14
  • 41
  • 49

4 Answers4

4

Check out mysql_error function.

Babiker
  • 18,300
  • 28
  • 78
  • 125
  • 1
    `mysqli_error` error actually: `mysqli_query($db_conn, $query)` – onteria_ May 22 '11 at 20:25
  • @onteria_ : I was trying mysqli_error in the procedural format for the above connection and query, but it is always returning error saying mysqli_error needs exactly one "xxxxx", but zero supplied... (where xxxx varies, sometimes object, sometimes string etc..).. Could you please help me out with the query here.. I would want it to give the php error message (and not my custome die message) when the querying fails. thanks a lot! – arun nair May 23 '11 at 06:25
1

Have a look at the manual page for mysqli_error. In the section "Procedural style" you'll find a complete example that shows how to set up the database connection and querying the database, both steps with error handling.

If you want detailed error information from your scripts you can put the lines

error_reporting( E_ALL );
ini_set('log_errors', true);
ini_set('error_log', '/tmp/php-errors.log');

at the start of your code. That way all errors coming from PHP will be written into the log file. Make sure that the path to the log file exists (/tmp is only an example) and is writable by the web server, otherwise the errors will be silently discarded.

As a side note: While the "or die()" pattern is good for small examples, you should not use it in production code.

chiborg
  • 26,978
  • 14
  • 97
  • 115
0

I prefer using PDO, and having PDO throw an exception.

Daniel
  • 7,006
  • 7
  • 43
  • 49
0

you could insert the query into the query page on phpmyadmin that is if you are using it. But this wont tell you if the errors are with the variables

Harry Martland
  • 604
  • 2
  • 14
  • 26