在此示例中,为什么get_result返回对象?
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) }
答案 0 :(得分:1)
根据官方文档(here),get_result()
返回用于成功执行SELECT查询的结果集。接下来,您必须使用例如$result->fetch_assoc()
从结果集中提取数据:
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo $row['user_id'];
}