Throwing a mysql_query into an array

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Throwing a mysql_query into an array

Post by impulse() »

If I use mysql_fetch_array and dump it in $foo for example, how do I retrieve selected row numbers or keys from the DB. At the moment if I use

Code: Select all

echo  "$foo['count']";
echo "$foo['whatever']";
It only echos the data from the first row of the DB.

Stephen,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Lose the double quotes, they aren't needed for this.

I think we'll need some more context to understand when and where you are setting $foo and when and where you are calling these echos.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Edit your query to select the desired result set, then cycle through them with a while loop:

Code: Select all

<?
$qry = "SELECT * FROM myTable";
$res = mysql_query($qry) or die('error: ' . mysql_error());
while($row = mysql_fetch_array($res))
{
  // do something with this row
  echo $row['id'];
}
?>
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

I was using $foo because I was just testing how arrays worked with MySQL results and foo came to mind.
When you do

Code: Select all

while ($x = mysql_fetch_array($myQueryVar))
Is that making the loop continue untill $x isn't equal to mysql_fetch_array($myQueryVal) because there are no other results for the variable to be able to be equal to the array?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

impulse() wrote:I was using $foo because I was just testing how arrays worked with MySQL results and foo came to mind.
When you do

Code: Select all

while ($x = mysql_fetch_array($myQueryVar))
Is that making the loop continue untill $x isn't equal to mysql_fetch_array($myQueryVal) because there are no other results for the variable to be able to be equal to the array?
No... = is an assignment operator, not an equality operator... all the = sign does is assign the result of mysql_fetch_array() to the variable $x. mysql_fetch_array increments an internal pointer, and when there is nothing else to iterate through it returns false, thereby breaking out of the loop
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There are three things going on in that snippet:
  1. mysql_fetch_array() is called with a supposedly valid result resource $myQueryVar.
  2. The return value from mysql_fetch_array() is stored into $x.
  3. the while construct attempts to evaluate that same return value to see if it's equal to (in value to) a boolean true.
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

Where does the increment of the array come into place? Does that work behind the scenes within mysql_fetch_array? Somewhere it must be saying to server that the row it's trying to pass into the variable has already been placed so move to the next line?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

The Manual wrote:mysql_fetch_array

(PHP 3, PHP 4, PHP 5)
mysql_fetch_array -- Fetch a result row as an associative array, a numeric array, or both
Description
array mysql_fetch_array ( resource result [, int result_type] )

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

Boy, that manual sure looks like an informative resource!! I'd even bet that they have a search function??!!?!... OMGWTFBBQ!&*!!111one THEY DO!!!!!!!11 how long has that been there??!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hehe yup... it's like the coolest thing ever! 8) :D
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

damn id be like the l33tist php hax0r ever if i knew that was there!!
Post Reply