Solved

php/MySQL not showing info on first record

Posted on 2010-08-18
10
245 Views
Last Modified: 2012-05-10
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 www.electroniclifegroupfinder.com

0
Comment
Question by:axessJosh
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 3

Expert Comment

by:dockhand
Comment Utility
Are you certain that there isn't any empty record in your data base?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 
LVL 4

Expert Comment

by:javaftper
Comment Utility
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.
0
 
LVL 2

Author Comment

by:axessJosh
Comment Utility
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'); ?>

<?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";

      break;    

    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";

      break;

    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

      break;

    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

      break;

    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

      break;

  }

  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.phone, 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<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;

}

-->

</style>

</head>



<body>

<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 ?>

</p>



<a href="index.php">Home</a>

</div><!-- end wrapper -->

</body>

</html>

<?php

mysql_free_result($rsAges);

?>

Open in new window

0
 
LVL 3

Expert Comment

by:wuff
Comment Utility
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?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:axessJosh
Comment Utility
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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
Comment Utility
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
0
 
LVL 9

Expert Comment

by:Snarfles
Comment Utility
$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.
0
 
LVL 2

Author Closing Comment

by:axessJosh
Comment Utility
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.
0
 
LVL 3

Expert Comment

by:wuff
Comment Utility
Yes, the while pattern is commonly used to go through the result set. However there's a difference between "do...while" and "while...do". The first one always executes the loop once and then checks the while condition(s). The "while...do" first checks and then executes the loop if all conditions are true.

I use "while...do" 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
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now