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; 

Upvotes: 0

Views: 89

Answers (3)

Scott Helme
Scott Helme

Reputation: 4799

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.

Upvotes: 0

zerokavn
zerokavn

Reputation: 1333

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; 

Upvotes: 0

SergkeiM
SergkeiM

Reputation: 4168

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);

Upvotes: 1

Related Questions