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!

4 Answers 4

4

Check out mysql_error function.

Sign up to request clarification or add additional context in comments.

2 Comments

mysqli_error error actually: mysqli_query($db_conn, $query)
@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!
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.

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.