简体   繁体   中英

Query Result to Array String

I Have a simple query:

$str = mysqli_query($con,"select no_rekening from tbl_rekening");
While($row= mysqli_fetch_array($sql))
{
$rek_array .= $row['no_rekening'];
}
echo $rek_array;

Result:

008-K001008-K002

I want query result convert to array string, like this:

'008-K001','008-K002'

If you want to add a comma, then you can just embed that into the string (bear in mind that $rek_array is actually a string, not an array):

rek_array .= $row['no_rekening'].",";

If you want to add those single quote marks too then it's the same idea:

rek_array .= "'".$row['no_rekening']."',";

Of course that will add a trailing comma at the end, which you may not want - but you could easily trim that afterwards if you need to.

I would propose something like this (sorry I didn't coded in PHP for century)

while ($row = mysql_fetch_array($result)) {
    $rek_array .= $row[$i].',';// append comma after each value you
}
// reassign the value to the variable 
// without final comma 
$rek_array = substr($string,0,-1);
echo $rek_array;

I do think it should work fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM