PHP mysql_connect失败,与MySQL的PDO连接工作正常

时间:2015-09-18 07:45:56

标签: php pdo mysql-connect

与sql server的pdo连接正常,这是代码:

try {
    $host = "tcp:hdl324kjh.database.windows.net, 1433";
    $user = "user@hdl324kjh";
    $pwd = "password";
    $db = "my_db";
    $conn = new PDO ("sqlsrv:Server = $host; Database = $db", $user, $pwd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = ("SELECT * FROM CUSTOMERS");
    $stmt = $conn->query("$sql");
    $row = $stmt->fetch();
    print_r($row);
    $conn = NULL;
} catch(Exception $e) {
    die(print_r($e));
}

但由于某种原因,mysql_connect似乎不起作用:

$mysql_hostname = "hdl324kjh.database.windows.net:1433";
$mysql_user = "user@hdl324kjh";
$mysql_password = "password";
$mysql_database = "my_db";
$prefix = "";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");

mysql_select_db($mysql_database, $bd) or die("Could not select database");

$sql = mysql_query("SELECT * FROM CUSTOMERS");
while ($row = mysql_fetch_array($sql)) {
    echo $row['NAME'];
}

以下是错误消息:

  

连接失败:php_network_getaddresses:getaddrinfo失败:没有这样的主机。

2 个答案:

答案 0 :(得分:0)

来吧,你无法首先使用mysql_connect连接到ms sql server。

关于这个错误,很明显 - 您运行代码的系统,对主机hdl324kjh.database.windows.net一无所知

答案 1 :(得分:0)

您的PDO代码实际上并未连接到MySQL服务器,而是连接到Microsoft SQL服务器。这两者是非常不同的东西。