get\u result返回对象

3j86kqsm  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(313)

为什么在本例中获取结果返回对象?

function db_connect() {
    $db_host='localhost';
    $db_name='contact_manager';
    $db_user='root';
    $db_pass='';

    $connection = new mysqli($db_host, $db_user, $db_pass, $db_name);
    return $connection;
} 

$connection = db_connect();
$query = $connection->prepare("SELECT * FROM users");
$query->execute();
$op = $query->get_result();
var_dump($op);

这是vardump:

object(mysqli_result)#3 (5) {
["current_field"]=> int(0) 
["field_count"]=> int(4) 
["lengths"]=> NULL 
["num_rows"]=> int(24) 
["type"]=> int(0) }
anhgbhbe

anhgbhbe1#

根据官方文件(此处), get_result() 为成功的选择查询返回结果集。接下来必须从结果集中提取数据,例如 $result->fetch_assoc() :

while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
    echo $row['user_id'];
}

相关问题