简体   繁体   中英

Multiple database row values in single variable

I am adding all names in to single variable but it is showing only one value last one.

my code is:

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; 

No u just assign variable use it to plus add a "." before equtaion

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

But I would suggest to use array so u can use for JS(if ajax) or php for more flexible things

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

just test with

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; 

You are resetting the variable to the value of $row['NAME'] on each iteration of the loop.

What you need to do is append the variable to the end of $csk :

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

The extra . indicates that you want to append the value to $csk .

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