PHP連接MYSQL(代碼)


<?php
$db_host='hostname is database server ';
$db_database='database name';
$db_username='username';
$db_password='password';
$connection=mysql_connect($db_host,$db_username,$db_password);//連接到數據庫
mysql_query("set names 'utf8'");//編碼轉化
if(!$connection){
die("could not connect to the database:</br>".mysql_error());//診斷連接錯誤

}
$db_selecct=mysql_select_db($db_database);//選擇數據庫
if(!$db_selecct)
{
die("could not to the database</br>".mysql_error());
}
$query="select * from msu ";//構建查詢語句
$result=mysql_query($query);//執行查詢
if(!$result)
{
die("could not to the database</br>".mysql_error());

}
// array mysql_fetch_row(resource $result);
while($result_row=mysql_fetch_row(($result)))//取出結果並顯示
{
$num=$result_row[0];
$age=$result_row[1];
$name=$result_row[2];
echo "<tr>";
echo "<td>$num</td>";
echo "<td>$age</td>";
echo "<td>$name</td>";
echo "</tr>";
}


mysql_close($connection);//關閉連接
?>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM