php 测试php/mysqli连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3678650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:40:27  来源:igfitidea点击:

Testing php / mysqli connection

phpubuntumysqli

提问by Blind Fish

I am looking for a way to test just the connection portion of a php / mysqli connection. I am migrating from a LAMP server build on Vista to the same on Ubuntu and am having fits getting mysqli to work. I know that all of the proper modules are installed, and PhpMyAdmin works flawlessly. I have migrated a site over and none of the mysqli connections are working. The error that I am getting is the "call to member function xxx() on non-object" that usually pops up when either the query itself is bad or the query is prepared from a bad connection. I know that the query itself is good because it works fine on the other server with the exact same database structure and data. That leaves me with the connection. I tried to write a very simple test connection and put it in a loop such as ..

我正在寻找一种方法来测试 php/mysqli 连接的连接部分。我正在从 Vista 上构建的 LAMP 服务器迁移到 Ubuntu 上的 LAMP 服务器,并且适合让 mysqli 工作。我知道所有正确的模块都已安装,并且 PhpMyAdmin 可以完美运行。我已经迁移了一个站点,但没有一个 mysqli 连接正常工作。我得到的错误是“在非对象上调用成员函数 xxx()”,通常在查询本身错误或从错误连接准备查询时弹出。我知道查询本身很好,因为它在具有完全相同的数据库结构和数据的另一台服务器上运行良好。这给我留下了联系。我尝试编写一个非常简单的测试连接并将其放入循环中,例如 ..

if(***connection here ***)
{ echo "connected"; }
else
{ echo "not connected"; }

It echoes "connected", which is great. But just to check I changed the password in the connection so that I knew it would not be able to connect and it still echoed "connected". So, the if / else test is clearly not the way to go....

它呼应了“连接”,这很棒。但只是为了检查我更改了连接中的密码,以便我知道它无法连接并且它仍然回显“已连接”。所以,if / else 测试显然不是要走的路......

回答by Lekensteyn

mysqli_connect()always returns a MySQLi object. To check for connection errors, use:

mysqli_connect()总是返回一个 MySQLi 对象。要检查连接错误,请使用:

$mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
   echo "Connected.";
}

回答by Cubiczx

For test php connection in you terminal execute:

对于终端中的测试 php 连接,执行:

$ php -r 'var_dump(mysqli_connect("localhost:/tmp/mysql.sock", "MYSQL_USER", "MYSQL_PASS",
     "DBNAME));'

回答by Marc B

You need more error handling on the various database calls, then. Quick/dirty method is to simply do

那么,您需要对各种数据库调用进行更多的错误处理。快速/肮脏的方法是简单地做

 $whatever = mysqli_somefunction(...) or die("MySQL error: ". mysqli_error());

All of the functions return boolean FALSE if an error occured, or an appropriate mysqli object with the results. Without the error checking, you'd be doing:

如果发生错误,所有函数都返回布尔值 FALSE,或者返回带有结果的适当 mysqli 对象。如果没有错误检查,您将执行以下操作:

 $result = $mysqli->query("blah blah will cause a syntax error");
 $data = $result->fetchRow();  // $result is "FALSE", not a mysqli_object, hence the "call to member on non-object"