【发布时间】:2023-03-12 19:19:01
【问题描述】:
过去两天左右,我一直在将函数转换为 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?
【问题讨论】: