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
https://forums.phpfreaks.com/topic/288204-array-problem/
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
https://forums.phpfreaks.com/topic/288204-array-problem/#findComment-1478014
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
https://forums.phpfreaks.com/topic/288204-array-problem/#findComment-1478032
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.