php-mysql脚本失败时如何获取详细的错误报告? [英] how to get a detailed error report when a php-mysql script fails?

查看:133
本文介绍了php-mysql脚本失败时如何获取详细的错误报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行php-mysql脚本的过程中获得详细的错误描述?

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".

我想从MySQL中获取实际错误(而不是我提到的自定义错误),这将使该情况更好地理解-是数据库问题还是服务器连接问题等.

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');

谢谢!

推荐答案

请查看 mysqli_error .在过程样式"部分中,您将找到一个完整的示例,其中显示了如何建立数据库连接和查询数据库,这两个步骤均带有错误处理.

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');

在代码开头的

.这样,来自PHP的所有错误都将被写入日志文件.确保日志文件的路径存在(/tmp仅是示例)并且可由Web服务器写入,否则错误将被静默丢弃.

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.

作为一个补充说明:虽然"or die()"模式适用于小示例,但您不应在生产代码中使用它.

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

这篇关于php-mysql脚本失败时如何获取详细的错误报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆