標籤:
/*//方式一:已經過時
//1.造一個串連(建立通道)
$db = mysql_connect("localhost","root","123");
//2.選擇要操作哪一個資料庫
mysql_select_db("mydb",$db);
//3.寫SQL語句
$sql = "select * from Info";
//4.執行SQL語句,返回結果集
$result = mysql_query($sql);
//5.從結果集中取資料
while($row = mysql_fetch_row($result))
{
var_dump($row);
}*/
//方式二:物件導向的方式
//1.造一個連線物件
//$db = new MySQLi("localhost","root","123","mydb");
//2.判斷串連是否出錯
/*if(mysqli_connect_error())
{
echo "串連失敗";
exit(); //退出程式
}*/
//!mysqli_connect_error() or die("串連失敗!");
//3.寫SQL語句
//$sql = "select count(*) from Info";
//$sql = "insert into Info values(‘p001‘,‘‘,‘‘,‘‘,‘‘)";
//4.執行SQL語句,查詢語句如果執行成功返回結果集對象,如果執行失敗返回false
//$result = $db->query($sql);
//5.從結果集中讀取資料
//if($result)
//{
//var_dump($result->fetch_row()); //返回一行資料的數組(索引數組)
/*while($row = $result->fetch_row())
{
var_dump($row);
}*/
//var_dump($result->fetch_assoc());//返回一行資料(關聯陣列)
//$shuju = $result->fetch_all();//返回所有資料(二維數組)
//var_dump($result->fetch_object());//返回一行資料(對象)
//echo $shuju[0][0];
//}
//1.造連線物件
$db = new MySQLi("localhost","root","cuisheng520","cui");
//2.判斷是否出錯
!mysqli_connect_error() or die("串連失敗!");
//3.寫SQL語句
$sql = "select * from nation";
//4.執行SQL語句
$result = $db->query($sql);
//5.取資料
if($result)
{
$attr = $result->fetch_all();
echo "<select>";
foreach($attr as $v)
{
echo "<option value=‘{$v[0]}‘>{$v[1]}</option>";
}
echo "</select>";
}
php資料庫訪問