Jump to content

array problem


bravo14

Recommended Posts

Hi

 

I am using the code below to generate an array

 

 
$sql="SELECT * 
FROM `tbl_heat` 
WHERE `heat` =21 AND `points` >= 2 
OR `heat` = 22 AND `points` >=2 
AND `card_id` = 2";
 
//echo $sql;
 
$result=mysql_query($sql) or die(mysql_error());
while($row=mysql_fetch_assoc($result)){
$riders[] = $row['rider_name'];
print_r ($riders);

 

However the array comes out like this when printed

 

Array ( [0] => Chris Harris ) Array ( [0] => Chris Harris [1] => Kenneth Bjerre ) Array ( [0] => Chris Harris [1] => Kenneth Bjerre [2] => Martin Smolinski ) Array ( [0] => Chris Harris [1] => Kenneth Bjerre [2] => Martin Smolinski [3] => Chris Holder )

 

I want it to come out like this 

 

Array ( [0] => Chris Harris [1] => Kenneth Bjerre [2] => Martin Smolinski [3] => Chris Holder )

 

What am I doing wrong?

Link to comment
Share on other sites

Do the print_r() after you have built the array, not every time you add an element.

while($row=mysql_fetch_assoc($result)){
    $riders[] = $row['rider_name'];
}
echo '<pre>', print_r ($riders, 1), '</pre>'; // output is easier to read this way

And you should use mysqli functions, not mysql, or you will soon be in for a lot of rewriting when the mysql functions are no longer available

Link to comment
Share on other sites

Do the print_r() after you have built the array, not every time you add an element.

while($row=mysql_fetch_assoc($result)){
    $riders[] = $row['rider_name'];
}
echo '<pre>', print_r ($riders, 1), '</pre>'; // output is easier to read this way

And you should use mysqli functions, not mysql, or you will soon be in for a lot of rewriting when the mysql functions are no longer available

 

PDO is also viable. I personally found it easier to learn.

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.