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

标签 php mysql string

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

相关文章:

Javascript函数的参数是单词 "Array"而不是mySQL行

PHP MySQLi 多查询准备语句

c++ string.find() 返回字符串的长度

python - 如何从列表中删除所有特定数据类型?

php - 使用 post 删除正确的数据

php - 在 Laravel 5.1 中使用 Entrust 中间件时出现异常

mysql - 无法理解如何在基本 MYSQL 解释示例中摆脱表扫描

php - 在同一个表中同时插入 MySQL : how to?

php - 处理类别和子类别 MySQL 或 JSON

string - 确定字符移动的数量以获得单词