gpt4 book ai didi

php mysql- 将 mysql_fetch_array() 结果放入一个字符串中,并用逗号分隔字符串

转载 作者:行者123 更新时间:2023-11-29 01:39:33 26 4
gpt4 key购买 nike

我这里有一个 mysql 表(请求):

request:
r_id item_no
1 1
13 10
22 20
33 30
55 40

现在,我正在使用 php 使用 mysql_fetch_array() 取出 r_id 并尝试将 mysql_fetch_array() 结果放入一个字符串中,也用逗号分割字符串.

<?php   
include('config.php'); //connect to mysql
function f(){
$sql = "SELECT r_id FROM `request` ";
$result=mysql_query($sql);
$string = '';
$i = 0;
while ($row = mysql_fetch_array($result)) {
$string .= $row[$i];
}
$newstring = implode(", ", preg_split("/[\0-9]+/", $string));
return $newstring ;
}

$get=f();
echo $get;
?>

但是我无法从我的 php 代码中获取正确的字符串。

我想得到这样的字符串

1,13,22,33,55

我该如何解决这个问题?

最佳答案

你不需要 split 它和内爆,只需要这样做:

while ($row = mysql_fetch_array($result)) {
$string .= $row[$i].',';// append comma after each value you append to the string
}
return substr($string,0,-1);// cut off the trailing comma from the final string

关于php mysql- 将 mysql_fetch_array() 结果放入一个字符串中,并用逗号分隔字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29508351/

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