0

I have a column of data from an SQL database which I am calling through PHP. I wish to convert these results in to a consecutive string. So, it's a case of converting the result (consisting of a series of strings from the columns) in to a string.

How would I go about this?

At the moment, when I print the data using

while($row = mysqli_fetch_array($result))
  {   
  echo  $row['title'];
  echo "<br>";
  }

it returns each item, but I wish to place these items into a string.

Thanks

4

3 に答える 3

2

-関数を使用してそれを行うことができimplodeます:

$titles = array();
while($row = mysqli_fetch_array($result))
{
    $titles[] = $row['title'];
}
$titlestring = implode(",", $titles);

別のオプションは、次のように文字列を自分で連結することです。

$titlestring = "";
while($row = mysqli_fetch_array($result))
{
  $titles .= $row['title'] . ", ";
}
$titlestring = substr($titlestring, 0, -2);

私の意見では、最初のオプションの方が優れています。

于 2013-08-11T13:06:34.707 に答える
0

MySQLはこれをはるかにうまく処理できます..

タイトル列にインデックスを付けて、このようなクエリを実行します。

SET SESSION group_concat_max_len = @@max_allowed_packet;  

SELECT GROUP_CONCAT(title) FROM database.table GROUP BY title ASC

返されるデータの例 title1,title2,title3

于 2013-08-11T16:46:36.877 に答える
0
while($row = mysqli_fetch_array($result))
{    
    $myString .=  $row['title'] . " ";
}
于 2013-08-11T13:10:31.880 に答える