mysql_fetch_object

(PHP 3, PHP 4, PHP 5)

mysql_fetch_object -- 從結果集中取得一行作為物件

說明

object mysql_fetch_object ( resource result )

返回根據所取得的行建立的對象,若果沒有更多行則返回 FALSE

mysql_fetch_object()mysql_fetch_array() 類似,只有一點區別 - 返回一個物件而不是陣列。間接地也意味著只能通過欄位名來訪問陣列,而不是偏移量(數字是合法的屬性名)。

注: 本函數返回的欄位名是區分大小寫的。

<?php

/* this is valid */
echo $row->field;
/* this is invalid */
echo $row->0;

?>

速度上,本函數和 mysql_fetch_array() 一樣,也幾乎和 mysql_fetch_row() 一樣快(差別很不明顯)。

例子 1. mysql_fetch_object() 例子

<?php
mysql_connect
("hostname""user""password");
mysql_select_db("mydb");
$result mysql_query("select * from mytable");
while (
$row mysql_fetch_object($result)) {
    echo 
$row->user_id;
    echo 
$row->fullname;
}
mysql_free_result($result);
?>

參見 mysql_fetch_array()mysql_fetch_assoc()mysql_fetch_row()