在过去两天左右的时间里,我一直在将我的函数转换为 mysqli。我遇到了一个问题。我有一个函数,它返回一个包含数据库中一行的数组。但是,我希望数组包含多行而不是一行。此外,我将如何回应各个帖子。这是我失败的尝试,仅在数组中显示一行。
$mysqli = new mysqli("localhost", "user", "password", "database");
function display_posts ($mysqli, $user_id) {
$fields = "`primary_id`, `poster_id`, `profile_user_id`, `post`";
$user_id = (int)$user_id;
$query = "SELECT DISTINCT $fields FROM `posts` WHERE `profile_user_id` = $user_id
LIMIT 4";
if ($result = $mysqli->query($query)) {
$row = $result->fetch_assoc();
return $row;
$result->free();
$stmt->close();
}}
在这里,我试图显示数据。
$user_id = 1;
$posts = display_posts($mysqli, $user_id);
//Not sure what to do with $posts. A While loop perhaps to display each post?