本文實(shí)例講述了PHP使用mysql與mysqli連接Mysql數(shù)據(jù)庫(kù)的方法。分享給大家供大家參考,具體如下:
代碼很簡(jiǎn)單直接上了
<?php
/**
* @Author: HTL
* @Description: Description
*/
// 降低PHP默認(rèn)的錯(cuò)誤級(jí)別
// 只顯示除禁用以外的所有錯(cuò)誤
// 解決因?yàn)镻HP5.3+版本太高而導(dǎo)致在使用mysql_connect時(shí)出現(xiàn)的棄用警告“Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead”
error_reporting(E_ALL ^ E_DEPRECATED);
$db_host="localhost";
$db_user="root";
$db_passwd="";
$db_name="mysql";
echo("<BR>---------------------------mysql_connect------------------------<BR><BR>");
$query = "SELECT SESSION_USER(), CURRENT_USER(),now();";
$conn = mysql_connect($db_host, $db_user,$db_passwd);
if (!$conn)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_name, $conn);
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
var_dump($row);
}
mysql_close($conn);
echo("<BR>---------------------------mysqli_connect------------------------<BR><BR>");
$conn = mysqli_connect($db_host, $db_user, $db_passwd,$db_name) ;
if (!$conn)
{
die('Could not connect: ' . mysqli_error());
}
//execute the query.
$result = mysqli_query($conn, $query);
//display information:
while($row = mysqli_fetch_array($result)) {
var_dump($row);
}
mysqli_close($conn);
exit();
?>
有圖有真相
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。