使用mysqli连接错误

问题描述 投票:2回答:2

我有一个页面导航脚本,该脚本在我的网站上每页返回65条记录。在我的托管服务提供商将我转移到其他服务器之前,它一直工作良好。现在,当我转到页面时,出现以下错误:

Warning: mysqli::connect() [mysqli.connect]: (28000/1045): Access denied for user 'root'@'localhost' (using password: NO) in /home/jumbleweed/public_html/registry/browse_2004.php on line 10

Warning: mysqli::query() [mysqli.query]: invalid object or resource mysqli in /home/jumbleweed/public_html/registry/page.php on line 34

Line 9:  $db = new mysqli('localhost', 'my_username', 'my_password', 'my_database');
Line 10: $db->connect();

page.php的第34行:

public function countRecords() {
            //returns the number of records
            global $db;
            $count_query = "SELECT COUNT(*) FROM (".$this->query.") as temp";
Line 34:    $result = $db->query($count_query);
            $row = $result->fetch_row();
            return $row[0];
        }

很明显,我已经填写了正确的登录凭据以连接到数据库,但是它似乎忽略了它们,并尝试使用root作为用户名登录。

这是我的托管服务提供商的问题,以及他们如何设置服务器?

php mysqli database-connection
2个回答
1
投票

由于您正在使用MySQLi构造函数,因此无需使用connect()方法,因为它会自动连接到数据库。

删除此行,它应该起作用...

Line 10: $db->connect();

0
投票

是的,根据评论者,我将-> connect()调用删除。这就是破坏您的代码的原因。并切换到localhost,因此它使用套接字而不是IP连接。

© www.soinside.com 2019 - 2024. All rights reserved.