fetch_object
返回结果集中的当前行,作为一个对象。
fetch_object()
/ mysqli_fetch_object()
函数以对象的形式返回结果集中的当前行。
注意:从该函数返回的字段名是区分大小写的。
返回结果集中的当前行,然后打印每个字段的值:
<?php $mysqli = new mysqli("localhost","my_user","my_password","my_db"); if ($mysqli -> connect_errno) { echo "Failed to connect to MySQL: " . $mysqli -> connect_error; exit(); } $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; if ($result = $mysqli -> query($sql)) { while ($obj = $result -> fetch_object()) { printf("%s (%s)\n", $obj->Lastname, $obj->Age); } $result -> free_result(); } $mysqli -> close(); ?>
返回结果集中的当前行,然后打印每个字段的值:
<?php $con = mysqli_connect("localhost","my_user","my_password","my_db"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); exit(); } $sql = "SELECT Lastname, Age FROM Persons ORDER BY Lastname"; if ($result = mysqli_query($con, $sql)) { while ($obj = mysqli_fetch_object($result)) { printf("%s (%s)\n", $obj->Lastname, $obj->Age); } mysqli_free_result($result); } mysqli_close($con); ?>