Array only returning one result? (Should return 2)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Array only returning one result? (Should return 2)

Post by TheBrandon »

Hello all,

This is my first time toying with functions returning arrays so I'm sure the answer is simple, but please help.

I have a function doing my query:

Code: Select all

	function fetch_category_discounts($cat_ID) {
			mysql_connect(SQL_HOST_NAME, SQL_USER_NAME, SQL_PASSWORD) or die(mysql_error());
			mysql_select_db(SQL_DATABASE) or die(mysql_error());
			$result = mysql_query("SELECT * FROM discounts, category_relations WHERE category_relations.member_of = '$cat_ID' AND category_relations.id = discounts.id")or die(mysql_error());
			return mysql_fetch_assoc($result);
	}
Then I have my code using the function:

Code: Select all

		$discounts = fetch_category_discounts($cat_ID);
		print_r($discounts);
It's returning:

Code: Select all

Array ( [id] => 1 [short_discount] => 15% Off International Flights [long_discount] => 15% Off all International Flights. Coach seating only. Must wear cool sunglasses when in flight. [merchant] => Delta [expiration] => 1277494242 [member_of] => 1 )
However if you run the same query in MySQL you get:

Code: Select all

id	short_discount	long_discount	merchant	expiration	id	member_of
1	15% Off International Flights	15% Off all International Flights. Coach seating o...	Delta	1277494242	1	1
2	Testing Short Discount	I am a longer discount because I am the long_disco...	HIM	 	2	1
Where am I going wrong?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Array only returning one result? (Should return 2)

Post by requinix »

Code: Select all

return mysql_fetch_assoc($result);
Your code only gets one row. If you want all of them then I suggest a 2D array and a while loop.
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Array only returning one result? (Should return 2)

Post by TheBrandon »

I thought that would dump all of the results into an associative array?

Can you please give me an example of how the code should be modified?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Array only returning one result? (Should return 2)

Post by flying_circus »

Try something like this

Code: Select all

	function fetch_category_discounts($cat_ID) {
			mysql_connect(SQL_HOST_NAME, SQL_USER_NAME, SQL_PASSWORD) or die(mysql_error());
			mysql_select_db(SQL_DATABASE) or die(mysql_error());
			$result = mysql_query("SELECT * FROM discounts, category_relations WHERE category_relations.member_of = '$cat_ID' AND category_relations.id = discounts.id")or die(mysql_error());

			$aOutput = array();
			while($row = mysql_fetch_assoc($result))
				$aOutput[] = $row;

			return $aOutput;
	}
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Array only returning one result? (Should return 2)

Post by TheBrandon »

That did it. So mysql_fetch_assoc only returns a single entry?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Array only returning one result? (Should return 2)

Post by flying_circus »

Yes.

mysql_query returns a result set.

mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_row, etc all return 1 row of a result set.

You will need to use a loop to iterate through all the rows of a result set. :)
TheBrandon
Forum Commoner
Posts: 87
Joined: Tue May 20, 2008 8:55 am

Re: Array only returning one result? (Should return 2)

Post by TheBrandon »

Thank you for the help and explanation. I really appreciate it.
Post Reply