Home >> FAQs/Tutorials >> MySQL Tutorials

MySQL FAQs - Managing Tables and Running Queries with PHP Scripts

By: FYIcenter.com

Part:   1  2  3   4  5  6  7  8 

(Continued from previous part...)

What Is a Result Set Object?

A result set object is a logical representation of data rows returned by mysql_query() function on SELECT statements. Every result set object has an internal pointer used to identify the current row in the result set. Once you get a result set object, you can use the following functions to retrieve detail information:

  • mysql_free_result($res) - Closes this result set object.
  • mysql_num_rows($res) - Returns the number rows in the result set.
  • mysql_num_fields($res) - Returns the number fields in the result set.
  • mysql_fetch_row($res) - Returns an array contains the current row indexed by field position numbers.
  • mysql_fetch_assoc($res) - Returns an array contains the current row indexed by field names.
  • mysql_fetch_array($res) - Returns an array contains the current row with double indexes: field position numbers and filed names.
  • mysql_fetch_lengths($res) - Returns an array contains lengths of all fields in the last row returned.
  • mysql_field_name($res, $i) - Returns the name of the field of the specified index.

How To Query Tables and Loop through the Returning Rows?

The best way to query tables and loop through the returning rows is to run the SELECT statement with the mysql_query() function, catch the returning object as a result set, and loop through the result with the mysql_fetch_assoc() function in a while loop as shown in the following sample PHP script:

<?php
  include "mysql_connection.php";

  $sql = "SELECT id, url, time FROM fyi_links";
  $res = mysql_query($sql, $con);
  while ($row = mysql_fetch_assoc($res)) {
    print($row['id'].",".$row['url'].",".$row['time']."\n");
  }
  mysql_free_result($res);

  mysql_close($con); 
?>

Using mysql_fetch_assoc() is better than other fetch functions, because it allows you to access field values by field names. If you run this script, you will see all rows from the fyi_links table are printed on the screen:

101, dev.fyicenter.com, 2006-07-01 22:29:02
102, dba.fyicenter.com, 2006-07-01 22:29:02
1101, dev.fyicenter.com, 2006-07-01 22:29:02
1102, dba.fyicenter.com, 2006-07-01 22:29:02

Don't forget to call mysql_free_result($res). It is important to free up result set objects as soon as you are done with them.

How To Break Output into Pages?

If you have a query that returns hundreds of rows, and you don't want to present all of them to your users on a single page. You can break output into multiple pages, and only present 10 rows per page like what Google Website is doing. To do this, you need to modify your query with the LIMIT clause to return rows starting from the correct row number, and limited to 10 rows in the output.

The tutorial exercise below shows you how to break output into pages with 2 rows per page. It also calcualtes the total number of pages with a simple query criteria and order by condition. As an example, rows for page number 2 are returned.

<?php
  include "mysql_connection.php";
 
  $rowsPerPage = 2;
  $where = " WHERE url LIKE '%co%'";
  $order = " ORDER BY time DESC";
  
  $curPage = 2; 
  $start = ($curPage-1) * $rowsPerPage; 

  $sql = "SELECT COUNT(*) AS count FROM fyi_links" . $where . $order;
  print("SQL = $sql\n");
  $rs = mysql_query($sql, $con);
  $row = mysql_fetch_assoc($rs);
  $numberOfPages = $row['count'] / $rowsPerPage; 
  print("Number of pages = $numberOfPages\n");
  mysql_free_result($rs);
  
  $sql = "SELECT * FROM fyi_links" . $where . $order
    . " LIMIT ".$start.", ".$rowsPerPage;
  print("SQL = $sql\n");
  $rs = mysql_query($sql, $con);
  while ($row = mysql_fetch_assoc($rs)) {
    print($row['id'].", ".$row['url'].", "
    . $row['notes'].", ".$row['time']."\n");
  }
  mysql_free_result($rs);

  mysql_close($con); 
?>

If you run this script, you will get something like this:

SQL = SELECT COUNT(*) AS count FROM fyi_links 
   WHERE url LIKE '%co%' ORDER BY time DESC
Number of pages = 3

SQL = SELECT * FROM fyi_links WHERE url LIKE '%co%' 
   ORDER BY time DESC LIMIT 2, 2
101, dev.fyicenter.com, , 2006-07-01 20:24:46
102, dba.fyicenter.com, Nice site., 2006-07-01 20:24:46

(Continued on next part...)

Part:   1  2  3   4  5  6  7  8 

MySQL Tutorials:

More...


Other Tutorials/FAQs:

More...


Related Resources:

More...


Selected Jobs:

More...