Jump to content

Simple SQL select query only returning one row? :(


Recommended Posts

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 :)

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
};

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.