简体   繁体   中英

PHP mysql_connect fails, PDO connection to MySQL works fine

The pdo connection to the sql server works fine, here is the code:

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));
}

But for some reason the mysql_connect doesn't seem to work:

$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'];
}

Here is the error message:

Connection failed: php_network_getaddresses: getaddrinfo failed: No such host is known.

Come on, you cannot connect to ms sql server using mysql_connect in the first place.

While regarding this error, it is pretty clear - the system you are running your code on, knows nothing of the host hdl324kjh.database.windows.net

Your PDO code does not actually connect to a MySQL server, but a Microsoft SQL server. The two are very different things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM