php - 从列数据库表中回显数组列表
问题描述
我正在尝试从列中回显所有行的列表userid
并用逗号分隔它们并从最后一个值中删除最后一个逗号
$sql_userid = "SELECT `userid` FROM `tabe_users`";
$result_userid = $con->query($sql_userid);
if ($result_userid->num_rows > 0) {
while($row_userid = $result_push->fetch_assoc()) {
$getallids = mysqli_free_result($result_userid) . ', ';
$listallids = substr($getallids, 0, -2);
}
}
echo $listallids;
解决方案
用这个更新你的代码,你不能在 echo 中打印数组,如果你想在 echo 中打印,那么你必须在再次使用它解码时将数组编码为 json。
或者干脆使用print_r()
$sql_userid = "SELECT `userid` FROM `tabe_users`";
$result_userid = $con->query($sql_userid);
$listallids = array();
if ($result_userid->num_rows > 0) {
while($row_userid = $result_push->fetch_assoc()) {
$getallids = mysqli_free_result($result_userid) . ', ';
$listallids[] = substr($getallids, 0, -2);
}
}
echo json_encode($listallids);
// or
print_r($listallids);
推荐阅读
- java - Mojo Codehaus 属性插件替代品
- python - 如何读取配置文件?
- amazon-dynamodb - 使用 DynamoDBMapper 注释映射 Java 可选
- linkedin - Linkedin 广告报告返回给定广告系列的空“元素”数组
- excel - Excel 图表:有一种方法可以更改 Y 轴值,将“掩码”文本放在数字位置?
- java - 致命异常尝试在空对象引用上调用虚拟方法“java.lang.String java.lang.Object.toString()”
- r - 为什么即使我没有使用 list()、key<-、names<- 或 attr<-,我也会收到“检测到无效的 .internal.selfref”警告(但没有输出)?
- python - 列出索引超出范围的读取文件
- jsp - 如何使用 html 文件连接服务器?
- java - 一组数字的共同值