我无法连接到mysql

时间:2016-06-14 15:15:54

标签: php mysql

我已经在az.pl上创建了一个数据库我想使用在其他网站上完美运行的旧代码。我收到此错误消息:

  

无法连接到mysql

这是我的代码:

$dbh = mysqli_connect("localhost","user","password", "db") or die ("could not connect to mysql");

我也尝试过:

$c = mysql_connect("localhost", "user", "password");
mysql_select_db("db");
$result = mysql_query("SELECT 'Hello, dear MySQL user!' AS _message FROM DUAL");
$row = mysql_fetch_assoc($result);
echo htmlentities($row['_message']);

两者都有相同的结果,错误信息。

我在网上搜索过但我还没找到确切的问题。

2 个答案:

答案 0 :(得分:0)

在调试连接问题时,请考虑使用文档:

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

<?php
$link = mysqli_connect("localhost", "user", "password", "db");

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

    // this will tell you why the connection failed
    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 :(得分:0)

我终于连接到了数据库。我需要做的就是在以下代码中将isset更改为:

if (!$_GET['pid']) {
    $pageid = '1';
} else {
    $pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
// Query the body section for the proper page
$sqlCommand = "SELECT pagebody FROM pages WHERE id='$pageid' LIMIT 1"; 
$query = mysqli_query($link, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $body = $row["pagebody"];
}

我使用MonkeyZeus代码连接数据库。谢谢你MonkeyZeus。

相关问题