2016-12-29 37 views
-1

,我有以下的PHP代碼連接我的數據庫mysql_connect()函數,但我不斷收到以下警告:更新貶值的mysql_connect()

推薦使用:mysql_connect()函數:mysql擴展已被棄用,並且將在被刪除未來:使用mysqli或PDO代替.....

什麼會正確的方式來更新此連接,所以我沒有任何問題在未來?

<?php 
 
//CREATED BY ... 
 
/* 
 
1: "die()" will exit the script and show an error statement if something goes wrong 
 
2: A "mysql_connect()" error usually means your username/password are wrong 
 
3. A "mysql_select_db()" error usually means the database does not exist 
 
*/ 
 
//Place db host name. Sometimes "localhost" but 
 
//sometimes looks like this: >> ???mysql??.someserver.net 
 
$db_host = "localhost"; 
 
//Place the username for the MySQL database here 
 
$db_username = "..."; 
 
//Place the password here: 
 
$db_pass = "..."; 
 
//Place the name for the MyS 
 
$db_name = "..."; 
 

 
//Run the connection right here! 
 
mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect"); 
 
mysql_select_db("$db_name") or die("no databases"); 
 
?>

+1

使用'mysqli'或'PDO'重新編碼腳本。 – Barmar

+2

*所有* mysql_ *函數都被棄用。您需要在代碼中替換它們中的所有*,而不僅僅是幾個。 –

+0

https://www.sitepoint.com/migrate-from-the-mysql-extension-to-pdo/ –

回答

0
try { 
    $dbh = new PDO("mysql:dbname=$db_name;host=$db_host", $db_username, $db_pass); 
} catch (PDOException $e) { 
    echo 'Connection failed: ' . $e->getMessage(); 
} 

更多信息看這裏http://php.net/manual/de/pdo.prepare.php

+0

他要求連接和選擇功能,而不是更多!更多他可以閱讀PHP文檔! – Eugen

+0

好,你的鏈接到sitepoint幫助更多...遠遠超過php pdo文檔: - / – Eugen