bravo14 Posted May 3, 2014 Share Posted May 3, 2014 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? Quote Link to comment Share on other sites More sharing options...
jperez260 Posted May 3, 2014 Share Posted May 3, 2014 where is your closing curly brace? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 3, 2014 Share Posted May 3, 2014 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 Quote Link to comment Share on other sites More sharing options...
TrickyInt Posted May 3, 2014 Share Posted May 3, 2014 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. Quote Link to comment Share on other sites More sharing options...
bravo14 Posted May 3, 2014 Author Share Posted May 3, 2014 I understand I need to look at other sources e.g. mysqli or PDO, I suppose old habits die hard :-) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.