且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 MySqli 和数组返回多行

更新时间:2023-12-04 12:18:10

你必须使用循环来一次性获取它们:

You have to use a loop to get them all at once:

<?php
function resultToArray($result) {
    $rows = array();
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    return $rows;
}

// Usage
$query = 'SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id LIMIT 4';
$result = $mysqli->query($query);
$rows = resultToArray($result);
var_dump($rows); // Array of rows
$result->free();