问题
I have a page navigation script that returns 65 records per page on my web site. It used to work fine until my hosting provider moved me to a different server. Now when I go to the page, I get the following error:
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();
Line 34 of page.php:
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];
}
Obviously, I have the correct login credentials filled in to connect to the database, but it seems to be ignoring them and trying to login with root as the username.
Is this an issue with my hosting provider and how they have the server set up??
回答1:
Since you are using the MySQLi constructor there is no need to use the connect()
method since it will automatically connect to the database.
Remove this line and it should work...
Line 10: $db->connect();
回答2:
Yeah, as per the commenter, I'd drop the ->connect() call. It's what's breaking your code. And switch to localhost, so it uses sockets instead of IP connection.
来源:https://stackoverflow.com/questions/13975659/connect-error-with-mysqli