Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through a mysql result set

What are some different ways to loop through a mysql result set? I'm new to PHP and MySQL so I'm looking for simple ways to loop through and an explanation as to how the provided code works.

like image 991
sassy_geekette Avatar asked Nov 18 '09 15:11

sassy_geekette


2 Answers

the first example that comes to my mind:

    <?php

    $link = mysql_connect(/*arguments here*/);

    $query = sprintf("select * from table");

    $result = mysql_query($query, $link);

    if ($result) {
      while($row = mysql_fetch_array($result)) {
        // do something with the $row
      }

    }
    else {
      echo mysql_error();
    }
?>
like image 164
Gabriel Sosa Avatar answered Sep 19 '22 16:09

Gabriel Sosa


Here is a full example:

http://php.net/manual/en/mysqli-result.fetch-array.php

  1. Connect
  2. Select database
  3. Make query
  4. Cycle on the result and fetch array to get the row
like image 33
Palantir Avatar answered Sep 22 '22 16:09

Palantir