我这里有一个 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/