gpt4 book ai didi

PHP/MySQL - 如何从表中选择 id 并在数组中回显

转载 作者:行者123 更新时间:2023-11-29 01:49:36 24 4
gpt4 key购买 nike

如何制作我们从表中调用的id数组?

我想要的是这样的:

$array = array(1, 2, 3, 4, 5); // **1 - 5 select from a table**.

谢谢

代码:

$query = mysqli_query($conn, "SELECT * FROM tableA");
while($row = mysqli_fetch_assoc($query )){
$a = implode(',',(array)$row['id_add_user']);
echo $a;
}

我从 echo $a 得到的是 12345 而不是 1,2,3,4,5

最佳答案

将所有元素添加到一个数组中,然后在获取所有结果后将其implode() 与您想要的分隔符(此处为它的", ")一起放入一个字符串中.

$result = [];
$query = mysqli_query($conn, "SELECT * FROM tableA");
while($row = mysqli_fetch_assoc($query)){
$result[] = $row['id_add_user']);
}

echo implode(", ", $result);

关于PHP/MySQL - 如何从表中选择 id 并在数组中回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50287001/

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