如何在不同环境中获取MySQLi错误信息

时间:2021-06-06 23:26:43

In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:

在我的本地/开发环境中,MySQLi查询执行正常。但是,当我在我的Web主机环境中上传它时,我收到此错误:

Fatal error: Call to a member function bind_param() on a non-object in...

致命错误:在...中的非对象上调用成员函数bind_param()

Here is the code:

这是代码:

global $mysqli;
$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);

To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.

为了检查我的查询,我尝试通过控制面板phpMyAdmin执行查询,结果是正常的。

1 个解决方案

#1


62  

First of all, always have this line before MySQLi connect in all your environments:

首先,在MySQLi连接所有环境之前,始终拥有此行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

After that all MySQL errors will be transferred into PHP exceptions. Uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. A stack trace will lead you to the exact spot where error occurred.

之后,所有MySQL错误都将转移到PHP异常中。反过来,未捕获的异常会导致PHP致命错误。因此,如果出现MySQL错误,您将收到传统的PHP错误。这将立即让您意识到错误原因。堆栈跟踪将引导您到达发生错误的确切位置。

Note that you have to be able to see PHP errors in general. And here indeed goes the matter of different environments:

请注意,您必须能够查看PHP错误。这确实是不同环境的问题:

  • On a live site you have to peek into error logs, so, settings have to be

    在实际站点上,您必须查看错误日志,因此必须进行设置

    error_reporting(E_ALL);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);
    
  • While on a local development server it's OK to make errors on the screen:

    在本地开发服务器上,可以在屏幕上显示错误:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

And a little list of what you should not:

还有一些你不应该做的清单:

  • Use error suppression operator (@)
  • 使用错误抑制运算符(@)
  • Use die() or echo or any other function to print the error message on screen unconditionally. PHP can echo it all right already, no assistance is required.
  • 使用die()或echo或任何其他函数无条件地在屏幕上打印错误消息。 PHP可以很好地回应它,不需要任何帮助。
  • Testing the query result manually (like if($result)) just makes no sense. Either your query failed and you will already get the error exception, or it was all right and there is nothing to test.
  • 手动测试查询结果(如if($ result))只是没有意义。您的查询失败并且您已经获得错误异常,或者它没有问题,并且没有任何可测试的内容。
  • Use try..catch for echoing the error message. Again PHP can do it better, way better.
  • 使用try..catch回显错误消息。 PHP可以做得更好,更好。

#1


62  

First of all, always have this line before MySQLi connect in all your environments:

首先,在MySQLi连接所有环境之前,始终拥有此行:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

After that all MySQL errors will be transferred into PHP exceptions. Uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. A stack trace will lead you to the exact spot where error occurred.

之后,所有MySQL错误都将转移到PHP异常中。反过来,未捕获的异常会导致PHP致命错误。因此,如果出现MySQL错误,您将收到传统的PHP错误。这将立即让您意识到错误原因。堆栈跟踪将引导您到达发生错误的确切位置。

Note that you have to be able to see PHP errors in general. And here indeed goes the matter of different environments:

请注意,您必须能够查看PHP错误。这确实是不同环境的问题:

  • On a live site you have to peek into error logs, so, settings have to be

    在实际站点上,您必须查看错误日志,因此必须进行设置

    error_reporting(E_ALL);
    ini_set('display_errors', 0);
    ini_set('log_errors', 1);
    
  • While on a local development server it's OK to make errors on the screen:

    在本地开发服务器上,可以在屏幕上显示错误:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

And a little list of what you should not:

还有一些你不应该做的清单:

  • Use error suppression operator (@)
  • 使用错误抑制运算符(@)
  • Use die() or echo or any other function to print the error message on screen unconditionally. PHP can echo it all right already, no assistance is required.
  • 使用die()或echo或任何其他函数无条件地在屏幕上打印错误消息。 PHP可以很好地回应它,不需要任何帮助。
  • Testing the query result manually (like if($result)) just makes no sense. Either your query failed and you will already get the error exception, or it was all right and there is nothing to test.
  • 手动测试查询结果(如if($ result))只是没有意义。您的查询失败并且您已经获得错误异常,或者它没有问题,并且没有任何可测试的内容。
  • Use try..catch for echoing the error message. Again PHP can do it better, way better.
  • 使用try..catch回显错误消息。 PHP可以做得更好,更好。