单个变量中的多个数据库行值

时间:2013-10-04 07:20:12

标签: php mysql

我将所有名称添加到单个变量中,但它只显示最后一个值。

我的代码是:

include 'dbconnect.php';
$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
//$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
while ($row = mysql_fetch_assoc($query)) {
$csk = "'".$row['NAME']."',";
}
echo $csk; 

3 个答案:

答案 0 :(得分:1)

不,你只需分配变量就可以使用它来加上一个“。”等式之前

  $csk .= "'".$row['NAME']."',";

但我建议使用数组,这样你就可以使用JS(如果ajax)或php来获得更灵活的东西

$csk = array();
while ($row = mysql_fetch_assoc($query)) {
$csk[] = array($row['NAME']);
}
echo $csk; //for ajax use echo json_encode($csk);

答案 1 :(得分:0)

只需用

进行测试
include 'dbconnect.php';
$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
//$result = mysql_query("SELECT * FROM bookedtates WHERE SID='$ServiceHosterIdv' AND BOOKEDDATE='$q'");
$csk = '';
while ($row = mysql_fetch_assoc($query)) {
    $csk .= "'".$row['NAME']."',";
}
echo $csk; 

答案 2 :(得分:0)

您正在循环的每次迭代中将变量重置为$row['NAME']的值。

您需要做的是将变量附加到$csk

的末尾
$csk .= "'".$row['NAME']."',";
     ^---- notice the extra . here

额外.表示您要将值附加到$csk