RabbitFactoryUK Posted July 3, 2011 Share Posted July 3, 2011 Hi all I'm in need of some help, this is probaly something really simple but I have been looking at it for a while now and well just can't see what the issue is and I'm hoping that a fresh set of eyes will help I have a table (sections), the table currently has 3 fields: section_id section_title section_desc There is currently 2 records in the table: 1 Blog Blog Entries 2 Tutorials Tutorial entries I have a function that is meant to get all the data from this table and return an array: function get_sections() { $result=mysql_query("SELECT * FROM sections"); $row[]=mysql_fetch_assoc($result); return $row; } and I am then using print_r to view the contents of this: $sections=get_sections(); print_r($sections); how ever even though there are 2 entries in this table the function is only returning one : Array ( [0] => Array ( [section_id] => 1 [section_title] => Blog [section_desc] => Blog Entries ) ) However I can access the other record if I add the WHERE section_id=2 clause onto the end of the SQL statement. Any ideas on what is going on here? Thanks in advance for any help and advice Quote Link to comment https://forums.phpfreaks.com/topic/240988-simple-sql-select-query-only-returning-one-row/ Share on other sites More sharing options...
wildteen88 Posted July 3, 2011 Share Posted July 3, 2011 If you query is returning more than one result you'll need to loop through the results function get_sections() { $result=mysql_query("SELECT * FROM sections"); $rows = array(); // use a loop to get all the results from the query while($row =mysql_fetch_assoc($result)) $rows[] = $row; // add the current row to the $rows array return $rows; // return all results }; Quote Link to comment https://forums.phpfreaks.com/topic/240988-simple-sql-select-query-only-returning-one-row/#findComment-1237853 Share on other sites More sharing options...
RabbitFactoryUK Posted July 3, 2011 Author Share Posted July 3, 2011 oh dear lol, I told you it would be something simple!!! I guess that what happens when you code for 3 hours straight, you miss things!!! Thank you very much for your quick response! and it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/240988-simple-sql-select-query-only-returning-one-row/#findComment-1237854 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.