gpt4 book ai didi

php - SQL查询如何从多行返回数据

转载 作者:行者123 更新时间:2023-11-29 10:47:59 25 4
gpt4 key购买 nike

<?php
$con = mysqli_connect("localhost","root","","register");
$sql = "SELECT * FROM photos WHERE user_id ='$id'";
$result = mysqli_query($con,$sql);
$array = mysqli_fetch_assoc($result);
$rowcount = mysqli_num_rows($result);
$source = $array['photo'];
?>

我想从列照片中获取数据,但如果我有多行具有相同的 user_id,$source = $array['photo'] 仅返回第一行的数据。如何从具有相同 user_id 的所有行获取数据?

我的数据库是:

+----+---------+----------+
| id | user_id | photo |
+----+---------+----------+
| 1 | 14 | 1010.jpg |
| 2 | 14 | 1013.jpg |
| 3 | 14 | 1210.jpg |
| 4 | 10 | 1173.jpg |
| 5 | 20 | 2038.jpg |
+----+---------+----------+

最佳答案

<?php
$con = mysqli_connect("localhost","root","","register");
$sql = "SELECT * FROM photos WHERE user_id ='$id'";
$result = mysqli_query($con,$sql);
$rowcount = mysqli_num_rows($result);

while ($row = mysqli_fetch_assoc($result)) {
$source = $row['photo];
// do stuff with $source
}
?>

查看有关 mysqli_fetch_assoc 的文档.

关于php - SQL查询如何从多行返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44229514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com