php/MySQL not showing info on first record

I built a small web app for a church to find sunday school classes.  When searched by age by clicking on the age range, it doesn't show the information for the first record.  

the site is at

Who is Participating?
Ray PaseurConnect With a Mentor Commented:
Line 38 runs the query.  

Line 39 removes the first row from the results set of that query.

Lines 68 through 72 is an iterator pattern that consumes the rest of the results set from that query.

Suggest you remove line 39.  As I said, it is a common error!

bet of luck with it, ~Ray
Are you certain that there isn't any empty record in your data base?
Ray PaseurCommented:
This is a common error.  Often the mysql_fetch_assoc() function is run once before the while() iterator.  Please post the PHP code that generates the page.  Thanks, ~Ray
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

PHP code is stored server side therefore to investigate this problem you will need to upload your groupsByAge.php file here.  
Once you've done this I shall investigate.
axessJoshAuthor Commented:
My code for that file is attached below.  There is data in that field because it shows up in the other query fields.  It is only the info for the first record shown in the PHP file.  Thanks in advance.
<?php require_once('Connections/conELF.php'); ?>
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  return $theValue;

$var2_rsAges = "-1";
if (isset($_GET['ageRangeID'])) {
  $var2_rsAges = $_GET['ageRangeID'];
mysql_select_db($database_conELF, $conELF);
$query_rsAges = sprintf("SELECT tblages.ageRangeID, tblages.ageDescription, tbltimes.timeID, tbltimes.timeDescription, tbltypes.typeID, tbltypes.typeDescription, tblgroup.groupID, tblgroup.firstName, tblgroup.lastName,, tblgroup.pictureLoc, tblgroup.typeID, tblgroup.ageRangeID, tblgroup.location, tblgroup.timeID FROM tblgroup, tblages, tbltimes, tbltypes WHERE tblgroup.typeID = tbltypes.typeID  AND tblgroup.ageRangeID = tblages.ageRangeID  AND tblgroup.timeID = tbltimes.timeID AND tblages.ageRangeID = %s", GetSQLValueString($var2_rsAges, "int"));
$rsAges = mysql_query($query_rsAges, $conELF) or die(mysql_error());
$row_rsAges = mysql_fetch_assoc($rsAges);
$totalRows_rsAges = mysql_num_rows($rsAges);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Groups Listed by Age</title>
<link href="mainStyles.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style2 {font-size: 14px}

#wrapper {
	overflow-y: scroll;

<div id="header"></div>
<div id="wrapper">
<h1>LIFE Group Finder</h1>
<h2>Groups Listed by Average Age</h2>

<h5>Click on the Teacher's Name for more information and to Register.</h5>

<?php if ($totalRows_rsAges > 0) { // Show if recordset not empty ?>
  <p><?php do { ?>
    <a href="groupDetail.php?groupID=<?php echo $row_rsAges['groupID']; ?>"><?php echo $row_rsAges['firstName']; ?> <?php echo $row_rsAges['lastName']; ?></a> <span class="style2">- <?php echo $row_rsAges['timeDescription']; ?> - <?php echo $row_rsAges['typeDescription']; ?></span>
    <hr />
    <br />
      <?php } while ($row_rsAges = mysql_fetch_assoc($rsAges)); ?>
  <?php } // Show if recordset not empty ?>
  <?php if ($totalRows_rsAges == 0) { // Show if recordset empty ?>
    We Are Sorry, there are currently no groups in that age range.
  <?php } // Show if recordset empty ?>

<a href="index.php">Home</a>
</div><!-- end wrapper -->

Open in new window

Check your SQL string. You are joining with several tables. Maybe the missing record has one of its field values missing in the table being joined.

Have you executed the SQL in phpAdmin to make sure your record is within the result set?
axessJoshAuthor Commented:
yes, it is there. and the SQL should be correct because the data is showing for all the other records in the results except the first.
$row_rsAges = mysql_fetch_assoc($rsAges);

this line steps the array of your results through to the first result... you then step to the second when you reach your do while loop. Since you don't need this before the do while loop you can comment out this line and it should work better.
axessJoshAuthor Commented:
I think i had this issue with the other pages before, but its been a long time since i've looked at this site i built.  I have redesigned it since to have much better SQL parsing to be much cleaner and more effective.
Yes, the while pattern is commonly used to go through the result set. However there's a difference between "do...while" and "". The first one always executes the loop once and then checks the while condition(s). The "" first checks and then executes the loop if all conditions are true.

I use "" since I then don't need the assignment at line 39 above.

I still can't understand why Josh' code didn't work.

At line 39 we get the first record
At line 68 we get the record data and print them
At line 72 we try to fetch record #2. If there are no more records we leave the loop. If there is another record we loop again.

Lines 68-72 are always executed. Is often a bit dangerous since you can easily guess what happens if there are no records in the result set. Luckily Josh has an "if..." to verify this in line 67
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.