1. 程式人生 > >php中mysqli_fetch_assoc()和mysqli_fetch_row()的區別

php中mysqli_fetch_assoc()和mysqli_fetch_row()的區別

使用mysqli_fetch_assoc()和mysqli_fetch_row()都是把查詢結果返回到一個數組中,都是返回第一行然後指標下移一行。 

區別:mysqli_fetch_assoc()用關鍵字索引取值。比如: 
$row = $result->fetch_assoc(); 
echo $row['username']; 

但是mysqli_fetch_row()用數字索引取值。比如: 
$row = $result->fetch_row(); 
echo $row[0];//注:“0”的意思是表中的第一個欄位(即username是表中的第一個欄位)。 

另外還有一個函式:mysqli_fetch_object()將一行取回到一個物件中,然後通過類的方式取值,比如: 

$row = $result->fetch_object(); 
echo $row->username;