Latest web development tutorials

PHP mysqli_fetch_object () function

PHP MySQLi Reference Manual PHP MySQLi Reference Manual

Returns the current row of the result set, then the output value of each field:

<?php 
// 假定数据库用户名:root,密码:123456,数据库:w3big 
$con=mysqli_connect("localhost","root","123456","w3big"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "连接 MySQL 失败: " . mysqli_connect_error(); 
} 

$sql="SELECT name,url FROM websites ORDER BY alexa";


if ($result=mysqli_query($con,$sql))
{
	while ($obj=mysqli_fetch_object($result))
	{
		printf("%s : %s",$obj->name,$obj->url);
		echo "<br>";
	}
	
	// 释放结果集合
	mysqli_free_result($result);
}

mysqli_close($con);
?>

Definition and Usage

mysqli_fetch_object () function to obtain the current row from the result set, and returns as an object.

Note: Field names returned by this function are case-sensitive.


grammar

mysqli_fetch_object( result,classname,params ) ;

参数 描述
result 必需。规定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的结果集标识符。
classname 可选。规定要实例化的类名称,设置属性并返回。
params 可选。规定一个传给 classname 对象构造器的参数数组。

technical details

return value: Returns object with a string property of the acquired line. If we do not focus more rows in the result returns NULL.
PHP version: 5+
Update log: Added function as distinct objects returned since PHP 5.0.0.


PHP MySQLi Reference Manual PHP MySQLi Reference Manual