当我尝试创建MySQLi对象的新实例以连接到本地数据库时,我收到以下错误:
Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in /opt/lampp/htdocs/login_system/php/dbconnect.php on line 9
以下是我的文件的代码:
<?php
//Database credentials
$server = 'localhost';
$username = 'root';
$password = 'root'; //NOT secure, used this as a demo
$database = 'login';
//Create the database object
$db = new mysqli($server, $username, $password, $database);
//Was there errors?
if($db->connect_errno > 0)
{
die("Error connecting to database " . $database + "\nErrors: " + $db->connect_error);
}
?>
有关为何发生这种情况的任何想法?
答案 0 :(得分:4)
您需要连接到“127.0.0.1”而不是“localhost”。
这里有一个解释:https://serverfault.com/questions/295285/mysql-cannot-connect-via-localhost-only-127-0-0-1
简而言之,您的服务器已配置,因此MySQL侦听127.0.0.1但不侦听localhost。