Jump to content

Array and implode problem


damo87

Recommended Posts

My query in the code below returns two rows (from my MySQL database), which it should do.  If I loop through the results, I can disply the two rows.  What I need to do is these two to be in a new variable, comma separated so that I can use them in an insert statement.

 

What I get with the code below is only the first row. If I print the array before I try to implode it, I get:

 

Array ( [number] => 1 )

 

So it looks like I'm not setting up the array correctly because the '1' is the value of the first row, but the value of the second row is missing.

 

Any suggestions for getting both values into the array, and them imploding them would be greatly appreciated!

 

$query= "Select round from rounds where rounds.round<=$roundineditable and rounds.round not in (select x from margin where user=$user and round<=$roundineditable)";
$result = mysql_query($query) or die("Query failed : " . mysql_error());
$row = mysql_fetch_assoc($result) or die(mysql_error());
$comma_separated = implode(",", $row);
echo "<p>$comma_separated</p>";

 

Link to comment
Share on other sites

Thanks for the push in the right direction. Got it to work like this:

 

$queryrounds = "Select number from ...........rest of query here"; 
$resultrounds = mysql_query($queryrounds) or die (mysql_error());
while($row = mysql_fetch_object($resultrounds)){
$rounds_array[] = $row->number;
}
$rounds_list = implode(", ", $rounds_array);
echo "Hello: $rounds_list"; 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.