无法找到数据库

时间:2016-04-09 04:23:46

标签: php sql database

我创建了一个php文件,它在我的服务器上。从chrome打开php文件后,它显示"无法找到数据库"。

<?php

    $dbhost = "fdb7.biz.nf";
    $database = "2065616_knurum";
    $username = "2065616_knurum";
    $dbpass = "XXXXXXXXX";

    mysqli_connect($dbhost, $username, $dbpass, $database);

    @mysql_select_db($database) or die("Unable to find database");

    $name = isset($_GET["name"]) ? $_GET["name"] : '';

    $message = isset($_GET["message"]) ? $_GET["message"] : '';


    $query = "INSERT INTO test VALUES ('', '$name', '$message')";

    mysql_query($query) or die (mysql_error("error"));

    mysql_close();

    echo"hello";
    ?>

经过多次帮助评论后,这里是正确的解决方案,并且有效。感谢所有回复的帮助者。

$dbhost = "fdb7.biz.nf";
$database = "2065616_knurum";
$username = "2065616_knurum";
$dbpass = "XXXXXXXX";

$link = mysqli_connect($dbhost, $username, $dbpass, $database);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}


$name = isset($_GET["name"]) ? $_GET["name"] : '';

$message = isset($_GET["message"]) ? $_GET["message"] : '';

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysqli_query($link, $query) or die (mysqli_error("error"));


echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
mysqli_close($link);
?>

4 个答案:

答案 0 :(得分:7)

http://php.net/manual/en/function.mysqli-connect.php

尝试此引用
<?php

$dbhost = "fdb7.biz.nf";
$database = "2065616_knurum";
$username = "2065616_knurum";
$dbpass = "XXXXXXXXX";

$link = mysqli_connect($dbhost, $username, $dbpass, $database);

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);

答案 1 :(得分:2)

{{1}}

答案 2 :(得分:2)

您正在混合使用库mysqlmysqli

选择后者用棍子

 @mysql_select_db($database) or die("Unable to find database");

应该是

 @mysqli_select_db($database) or die("Unable to find database");

答案 3 :(得分:2)

首先,您不能混合使用mysql_*函数和mysqli_*函数,后者更好,因为mysql_*已被弃用。

$conn = mysqli_connect($dbhost, $username, $dbpass, $database);

$query = "INSERT INTO test VALUES ('', '$name', '$message')";

mysqli_query($conn, $query) or die (mysqli_error("error"));

mysqli_close();

此外,您不应该禁止错误消息:

@mysqli_select_db($database) or die("Unable to find database");

删除@以启用错误报告。它在诊断语法错误方面非常有用。