faqts : Computers : Programming : Languages : PHP : Database Backed Sites : MySQL

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 2 people (100%) answered Yes
Recently 2 of 2 people (100%) answered Yes

Entry

If a query to a database returns records show that page but with no records show another

Nov 1st, 2004 13:50
Philip Olson, Phil Jennings,


In otherwords, if a number of rows (records) are returned, do one thing
(like show that information). And if no rows of information are
returned...do something else. First, go here:
  http://php.net/mysql-num-rows
The mysql_num_rows() function tells you how many rows exist from a
result set (MySQL Query), so, here's an example use:
<?php
  $sql = "SELECT name FROM data";
  $result = mysql_query($sql);
  if (!$result) {
      echo "Could not run query, doh: " . mysql_error();
      exit;
  }
  /* If less than one row is returned */
  if (mysql_num_rows($result) < 1) {
      echo "No rows returned, try again.";
  /* So, at least one row of data must have been returned */
  } else {
      while ($row = mysql_fetch_assoc($result)) {
          echo 'Hello ' . $row['name'];
      }
  }
?>
So, you may include different files depending on if rows exist, or call
certain functions, etc.