由于我试图提供尽可能格式正确的代码,因此我想到了这个问题。
通常,我为每个创建的准备好的语句检查数据库连接错误,例如:
if ($stmt = $mysqli->prepare("...random SQL code here...") {
//bind_param, executes, store_results, etc. here
} else {$err = "Database error";}
但是我从不检查执行时是否有数据库错误。我应该做吗?
它会解决更好的调试,并抛出更多有用的错误代码的机会更大,是否会进一步损害大型项目的性能?还是我应该完全忘记那些检查,而仅仅依靠mysql/php/apache日志?
谢谢你的协助。
最佳答案
是的,您应该测试连接的有效性,否,它不会影响脚本的性能。这些连接命令上的php文档几乎总是向您展示了如何。
看那里
http://php.net/manual/en/mysqli.construct.php
$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
.$mysqli->connect_error);
}