<?<span style="color: #000000;">php </span><span style="color: #800080;">$link </span>= <span style="color: #008080;">mysql_connect</span>('localhost', 'root', <span style="color: #000000;">”); </span><span style="color: #008080;">mysql_select_db</span>('abc', <span style="color: #800080;">$link</span><span style="color: #000000;">); </span><span style="color: #800080;">$sql</span> = “select *<span style="color: #000000;"> from book”; </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">; } </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_array</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">; } </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_object</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>->cid.'::'.<span style="color: #800080;">$row</span>->title.”<br><span style="color: #000000;">”; } </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>(<span style="color: #800080;">$sql</span><span style="color: #000000;">); </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$row</span> = <span style="color: #008080;">mysql_fetch_assoc</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$row</span>['cid'].'::'.<span style="color: #800080;">$row</span>[1].'<br>'<span style="color: #000000;">; } </span>?>
分析:
mysql_fetch_row,這個函數是從結果集中取一行作為枚舉數據,從和指定的結果標識關聯的結果集中取得一行數據並作為數組返回。每個結果的列儲存在一個陣列的單元中,偏移量從 0 開始。 注意,這裡是從0開始偏移,也就是說不能用欄位名字來取值,只能用索引來取值,所以如下程式碼是取不到值的:
while($row = mysql_fetch_row($res) ){
echo $row['cid'].'::'.$row[1].”;
} //這裡的$row['cid'] 取不到值。
my_fetch_array,從結果集中取得。一行作為關聯數組,或數字數組,或二者兼有,除了將資料以數字索引方式儲存在數組中之外,還可以將資料作為關聯索引儲存,用字段名作為鍵名。的結果像陣列一樣,可以用key或索引來取值,所以
while($row = mysql_fetch_array($res)){
echo $row['cid'].'::'.$row[1]. ”;
}//這裡$row['cid'],$row[1]都能得到對應的值。
mysql_fetch_object,顧名思義,從結果集中取得一行作為對象,並將字段名字做為屬性。所以只有這樣才能取到值
while($row = mysql_fetch_object($res)){
echo $row->cid.'::'.$row->title.";
取得一行作為關聯數組,也就是說這個函數不能像mysql_fetch_row那樣用索引來取值,只能用字段名字來取,所以
while($row = mysql_fetch_assoc($res)){
羅 Hrow'id '].'::'.$row[1].”;
} //$row[1]這樣是取不到值的
補充一點:
mysql_fetch_array函數是這樣定義的:array mysql_fetch_array (resource result [ , int result_type]),傳回根據從結果集取得的行所產生的數組,如果沒有更多行則傳回FALSE。 ,MYSQL_NUM 和MYSQL_BOTH。某種程度上可以算是mysql_fetch_row()與mysql_fetch_assoc()的集合。 >fetch_array($query);
$db是人資料庫操作類,$db->fetch_array($query),fetch_array($query)是那個db類別裡的方法,$row = $db->fetch_array($query )這句話的意思是從記錄集$query得到資料庫中的一行記錄。