Page 1 of 1
Throwing a mysql_query into an array
Posted: Sun Sep 24, 2006 12:13 pm
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,
Posted: Sun Sep 24, 2006 12:53 pm
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.
Posted: Sun Sep 24, 2006 3:34 pm
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'];
}
?>
Posted: Sun Sep 24, 2006 5:10 pm
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?
Posted: Sun Sep 24, 2006 5:14 pm
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
Posted: Sun Sep 24, 2006 5:17 pm
by feyd
There are three things going on in that snippet:
- mysql_fetch_array() is called with a supposedly valid result resource $myQueryVar.
- The return value from mysql_fetch_array() is stored into $x.
- the while construct attempts to evaluate that same return value to see if it's equal to (in value to) a boolean true.
Posted: Sun Sep 24, 2006 5:23 pm
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?
Posted: Mon Sep 25, 2006 12:21 am
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.
Posted: Mon Sep 25, 2006 12:34 am
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??!
Posted: Mon Sep 25, 2006 12:38 am
by Luke
hehe yup... it's like the coolest thing ever!

Posted: Mon Sep 25, 2006 12:41 am
by aaronhall
damn id be like the l33tist php hax0r ever if i knew that was there!!