MySQL 查询只抓取一行而不是多行.为什么


MySQL Query only grabbing one row instead of multiple. Why?

我正在尝试通过MySQL数据库中的这些行抓取多行:

$msg_sql = "SELECT * FROM ".TABLE_PREFIX."quotes ORDER BY rand(curdate()) LIMIT 3";
$msg_res = mysqli_fetch_assoc(mysqli_query($link, $msg_sql));
print_r($msg_res);

但是,我只得到 1 行。这是:

Array ( [id] => 1 [message] => test_message [Link] => link here ) 

我希望获得多行(所以多个 ID)

请告诉我我做错了什么。我仍然是MySQL的新手。

您必须为获取的每一行循环一次。

$result = mysqli_query($link, $msg_sql);
while ($item = mysqli_fetch_assoc($result)) {
    print_r($item);
}

您需要遍历结果:

$results = mysqli_query($link, $msg_sql);
while ($msg_res = mysqli_fetch_assoc($results))
{
    print_r($msg_res);
}