Returning Multiple Rows with MySqli and Arrays

后端 未结 3 1416
太阳男子
太阳男子 2020-11-30 13:20

For the past two days or so I\'ve been converting my functions to mysqli. I\'ve run into a problem. I have a function that returns an array containing a row from the databas

3条回答
  •  粉色の甜心
    2020-11-30 13:39

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

    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();
    

提交回复
热议问题