PHP如何输出数据库的每条数据

2025-04-13 20:54:59
推荐回答(1个)
回答1:

// 这是因为你从资源型结果集中获取数据时只获取了一次, 如果查询为多条数据应该迭代资源型结果集

$r = mysql_query($sql);  // 你的结果集

$result = [];
while (true) {
    $ary = mysql_fetch_assoc($r);  // 取出第一条数据, 数据指针向后移动一位
    if ($ary) {
        $result[] = $ary;   // 存储到结果数组中
    } else {
        break;  // 如果取出的结果为false, 则代表数据获取完毕, 终止循环
    }
}

echo '
';
print_r($result);   // 打印最终结果
echo '
';