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

Link to comment
Share on other sites

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

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.