how to get a detailed error report when a php-mysql script fails?

冷暖自知 提交于 2019-12-02 13:31:46

问题


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!


回答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.




回答2:


Check out mysql_error function.




回答3:


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




回答4:


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



来源:https://stackoverflow.com/questions/6090500/how-to-get-a-detailed-error-report-when-a-php-mysql-script-fails

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!