(PHP 4, PHP 5)
mysql_fetch_object — 锟接斤拷锟斤拷锟斤拷锟饺★拷锟揭伙拷锟斤拷锟轿拷锟斤拷锟�
$result
) : object
锟斤拷锟截革拷锟斤拷锟斤拷取锟矫碉拷锟斤拷锟斤拷锟缴的讹拷锟斤拷锟斤拷锟矫伙拷懈锟斤拷锟斤拷锟斤拷蚍祷锟� FALSE
锟斤拷
mysql_fetch_object() 锟斤拷 mysql_fetch_array() 锟斤拷锟狡o拷只锟斤拷一锟斤拷锟斤拷锟斤拷 - 锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷椤o拷锟接碉拷也锟斤拷味锟斤拷只锟斤拷通锟斤拷锟街讹拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷偏锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角合凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
Note: 锟剿猴拷锟斤拷锟斤拷锟截碉拷锟街讹拷锟斤拷锟斤拷小写锟斤拷锟斤拷锟斤拷
<?php
/* this is valid */
echo $row->field;
/* this is invalid */
echo $row->0;
?>
锟劫讹拷锟较o拷锟斤拷锟斤拷锟斤拷锟斤拷 mysql_fetch_array() 一锟斤拷锟斤拷也锟斤拷锟斤拷锟斤拷 mysql_fetch_row() 一锟斤拷锟届(锟斤拷锟杰诧拷锟斤拷锟皆o拷锟斤拷
Example #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()锟斤拷
result
resource 锟酵的斤拷锟斤拷锟斤拷锟斤拷私锟斤拷锟斤拷锟斤拷锟皆讹拷 mysql_query() 锟侥碉拷锟矫★拷
class_name
The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned.
params
An optional array of parameters to pass to the constructor
for class_name
objects.
Returns an object with string properties that correspond to the
fetched row, or FALSE
if there are no more rows.
锟芥本 | 说锟斤拷 |
---|---|
5.0.0 | Added the ability to return as a different object. |
Example #2 mysql_fetch_object() example
<?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);
?>
Example #3 mysql_fetch_object() example
<?php
class foo {
public $name;
}
mysql_connect("hostname", "user", "password");
mysql_select_db("mydb");
$result = mysql_query("select name from mytable limit 1");
$obj = mysql_fetch_object($result, 'foo');
var_dump($obj);
?>
Note: Performance
Speed-wise, the function is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row() (the difference is insignificant).
Note:
mysql_fetch_object() is similar to mysql_fetch_array(), with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).
Note: 锟剿猴拷锟斤拷锟斤拷锟截碉拷锟街讹拷锟斤拷锟斤拷小写锟斤拷锟斤拷锟斤拷
Note: 锟剿猴拷锟斤拷锟斤拷 NULL 锟街讹拷锟斤拷锟斤拷为 PHP
NULL
值锟斤拷