FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Looping through multiple mysql results





richard270384
Gday,

So I have a mysql data base and run a query in php which spits out the following info when i do a var_dump() (I'm retrieving the mysql results using mysql_fetch_array):

Code:

array(12) {
  [0]=>
  string(2) "12"
  ["artist_id"]=>
  string(2) "12"
  [1]=>
  string(19) "The Pussy Cat Dolls"
  ["artist_name"]=>
  string(19) "The Pussy Cat Dolls"
  [2]=>
  string(21) "www.pussycatdolls.com"
  ["artist_website"]=>
  string(21) "www.pussycatdolls.com"
  [3]=>
  string(1) "2"
  ["album_id"]=>
  string(1) "2"
  [4]=>
  string(4) "Don\"
  ["album_name"]=>
  string(4) "Don\"
  [5]=>
  string(4) "2004"
  ["album_releasedate"]=>
  string(4) "2004"
}


Please ignore the actual content - its just dummy content.

Now there should actually be a number of records. But I can only seem to access the first row of results. This is what PHPAdmin spits out when I run the same query:

Code:
artist_id  artist_name  artist_website  album_id  album_name  album_releasedate 
12 The Pussy Cat Dolls www.pussycatdolls.com 2 Don\ 2004
12 The Pussy Cat Dolls www.pussycatdolls.com 3 Don%5C%27t+Cha+10%5C%22 2004
12 The Pussy Cat Dolls www.pussycatdolls.com 4 Don%5C%27t+Cha+10%5C%22 2004
12 The Pussy Cat Dolls www.pussycatdolls.com 5 Don%5C%27t+Cha+%5C%2210 0000
12 The Pussy Cat Dolls www.pussycatdolls.com 6 Don%27t+Cha+10%22 0000


As you can see, there are 5 rows of results that should be returned.

Now I know this might seem like a silly problem that is no doubt an 'SQL/PHP 101' sort of thing, but i'm only just learning php and mysql. I know there are probably pre-written scripts to help me here but I like to "re-invent the wheel" when i'm learning so that i completely understand what is going on.

How do I loop through each row that the mysql query should return?

Cheers,
Richard Embarassed
MrBlueSky
You can do something like this:

Code:

while ($row = mysql_fetch_array($result)) {
        var_dump($row);
}


Where $result is the result of the sql-query.
dwikristianto
also,
make sure there is NO "limit" clause in your sql command.
this command will limit your result to specified number of rows.
richard270384
That works - thanks Mr BlueSky.

I think it might've just been late at night and my brain stopped working.

Dwikristianto - thanks for the tip.

Cheers,
Richard
Related topics
PlayStation 3
What Web Design Software is everyone using?
Cron job
Visual Basic Tutorial - Control Arrays: What, Why and How
PHP/(X)HTML Help
PHPBB Mods
php multidimension array question
Refine Search
multiple mysql connections
firefox 3 beta
"drawing" with JavaScript
Chained Selects
Updating multiple MySQL rows at once
Need info on large mysql databases
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.