PHP-无法连接到MySQL数据库

时间:2017-02-05 21:19:53

标签: php mysql mysqli

我有双重和三重检查我的数据库名称,服务器名称,mysql用户名和密码,我无法与数据库建立连接。当我去zeuspower.xyz/conn.php时,我看到“not connectedConnection failed:”但它不会给我任何mysql_error()。即使我输入了错误的密码也不会给我一个错误,我错过了什么吗?

我的连接文件如下:

<?php
$db_name = "zeusdb";
$mysql_username = "zeuspowerad";
$mysql_password = "Password";
$server_name = "zeuspower.xyz";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if ($conn == false) {
echo "not connected";
    die("Connection failed: " . mysql_error());
}
else{
echo "connected";
}
?>

更新的代码:

<?php
$db_name = "zeusdb";
$mysql_username = "zeuspowerad";
$mysql_password = "Password";
$server_name = "zeuspower.xyz";
$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if (mysqli_connect_errno()) {
echo "not connected";
    die("Connection failed: " . mysqli_error());
}
else{
echo "connected";
}
?>

2 个答案:

答案 0 :(得分:2)

您应该检查mysqli_connect_error()(使用)而不是mysql_error()

die("Connection failed: " . mysqli_connect_error());

答案 1 :(得分:0)

 $server_name = 'localhost';
 $mysql_username = 'zeuspowerad';
 $mysql_password = 'Password';
 $db_name = 'zeusd';

$conn = mysqli_connect($server_name, $mysql_username, $mysql_password, $db_name);
if ($conn == false) {
echo "not connected";
    die("Connection failed: " . mysqli_connect_error());
}
else{
echo "connected";
}

如果php脚本托管在servername zeuspower.xyz上,您可以尝试使用localhost作为主机参数,而不是“localhost”。希望这有效。