测试php/mysqli连接 [英] Testing php / mysqli connection

查看:75
本文介绍了测试php/mysqli连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来仅测试php/mysqli连接的连接部分.我正在从Vista上的LAMP服务器迁移到Ubuntu上的相同服务器,并且适合使mysqli正常工作.我知道所有正确的模块都已安装,并且PhpMyAdmin可以正常工作.我已经迁移了一个站点,并且没有mysqli连接正在工作.我得到的错误是在非对象上调用成员函数xxx()",当查询本身是错误的或从错误的连接准备查询时,通常会弹出该错误.我知道查询本身很好,因为它在具有完全相同的数据库结构和数据的其他服务器上可以正常工作.剩下的就是我的联系.我试图编写一个非常简单的测试连接,并将其放入诸如..

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

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

它呼应"connected",这很棒.但是只是检查一下,我更改了连接中的密码,以便我知道它将无法连接,并且仍然回显"connected".因此,if/else测试显然不是可行的方法....

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

推荐答案

mysqli_connect()始终返回一个MySQLi对象.要检查连接错误,请使用:

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

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

这篇关于测试php/mysqli连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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