我有这样的表:
table Database
+------+--------------+
| name | data |
+------+--------------+
| foo | certaindata1 |
| bar | certaindata2 |
| foo | certaindata3 |
+------+--------------+
我想输出数组值的数据,如:
array('certaindata1','certaindata2','certaindata3')
我试过这个
$sql = mysql_query("SELECT data FROM Database");
while($row=mysql_fetch_array($sql)){
echo $row['data'];
}
,结果为certaindata1certaindata2certaindata3
其中数组值应为'certaindata1','certaindata2','certaindata3'
答案 0 :(得分:2)
结果是预期的。您只是回显$row['data'];
而没有任何空格或换行符 - 因此结果将是certaindata1certaindata2certaindata3
。如果要将结果存储在数组中,可以执行以下操作:
while($row=mysql_fetch_array($sql)){
$yourArray[] = $row['data']; //storing the $row['data'] in an array
}
$comma_separated = implode(',', $yourArray);
print_r($comma_separated);
希望这有帮助!
答案 1 :(得分:0)
您应该使用implode()将其逗号分隔..
while($row=mysql_fetch_array($sql)){
$yourArray[] = $row['data']; //storing the $row['data'] in an array
}
$result = implode(",", $yourArray);
print_r($result);