Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Server Side Programming > PHP Programming
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 05-13-2003, 06:20 PM
  #1
tatlar
Paladin (Level 15)
 
tatlar's Avatar
 
Join Date: Feb 2002
Location: Surfside
Posts: 415
iTrader: (0)
tatlar is on a distinguished road
[MySQL & PHP] Returning records always off by one

Hi there,

I have some PHP code that pulls from a MySQL table. I pull both the total number of matching records and then the values of 3 fields of each record. In my HTML output I get the correct number of matching records, but the list of values is always short by one record.

For example, I send a query that should return 4 records. I get four records with my
$total_num_refs = mysql_num_rows ( $result ) ;
variable, but when I try and print out the field values in a table there is always one record missing. Here is the code (its part of an if/else statement that checks to see some form info has been submitted):

if ( $_POST['authors'] ) {
$k = $_POST['authors'] ;
$q = "SELECT Authors,Year,Title,ID FROM $tablename2 WHERE Authors LIKE '%k%' ORDER BY Year DESC" ;

$link_id = db_connect ( $default_dbname ) ;
if ( !$link_id ) error_message ( sql_error () ) ;

$result = mysql_query ( $q, $link_id ) ;
if ( !$result ) error_message ( sql_error () ) ;

$query_data = mysql_fetch_row ( $result ) ;
$total_num_refs = mysql_num_rows ( $result ) ;

if (!$total_num_refs) {
print "<p align=\"center\"><strong>No results found with those authors</strong></p>" ;
}

else {
echo "<center><strong>Your search for authors \"$k\" returned $total_num_refs references.</strong></center>\n" ;
echo "<br/><br/>\n" ;
echo "<table width=90% border=0 align=center cellpadding=4 cellspacing=0>\n" ;
echo "<tr>\n" ;
echo "<th width=40% NOWRAP class=forumHeader>Authors</th>\n" ;
echo "<th width=10% NOWRAP class=forumHeader>Year</th>\n" ;
echo "<th width=50% NOWRAP class=forumHeader>Title</th>\n" ;
echo "</tr>\n" ;
for ( $x=0; $x<$total_num_refs; $x++) {
$row = mysql_fetch_row( $result ) ;
$Authors = $row['Authors'];
$Year = $row['Year'];
$Title = $row['Title'];
$ID = $row['ID'];
echo "<tr>\n" ;
echo "<td width=\"40%\" class=\"forumTableLeft\"><a href=\"ref_detail.php?ID=$ID\">$Authors</a></td>\n" ;
echo "<td width=\"10%\" class=\"forumTable\" align=\"center\">$Year</td>\n" ;
echo "<td width=\"50%\" class=\"forumTableRight\">$Title</td>\n" ;
echo "</tr>\n" ;
}
echo "<tr>\n" ;
echo "<th width=40% NOWRAP class=forumHeader>Authors</th>\n" ;
echo "<th width=10% NOWRAP class=forumHeader>Year</th>\n" ;
echo "<th width=50% NOWRAP class=forumHeader>Title</th>\n" ;
echo "</tr>\n</table>\n" ;

}
}

Thanks in advance,

- Tatlar

Last edited by tatlar : 05-14-2003 at 02:02 PM.
tatlar is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-14-2003, 02:01 PM
  #2
tatlar
Paladin (Level 15)
 
tatlar's Avatar
 
Join Date: Feb 2002
Location: Surfside
Posts: 415
iTrader: (0)
tatlar is on a distinguished road
solution....

I got it to work. I took out this line:

$query_data = mysql_fetch_row ( $result ) ;

which I think somehow screwed the whole query up. I then inserted the query directly with the start of the output and called an array instead of using mysql_fetch_row, thus:

echo "<tr>\n" ;
echo "<th width=40% NOWRAP class=forumHeader>Authors</th>\n" ;
echo "<th width=10% NOWRAP class=forumHeader>Year</th>\n" ;
echo "<th width=50% NOWRAP class=forumHeader>Title</th>\n" ;
echo "</tr>\n" ;
while ( $resultset = mysql_fetch_array( $result) ) {
$Authors = $resultset['Authors'];
$Year = $resultset['Year'];
$Title = $resultset['Title'];
$ID = $resultset['ID'];
echo "<tr>\n" ;
echo "<td width=\"40%\" class=\"forumTableLeft\"><a href=\"ref_detail.php?
ID=$ID\">$Authors</a></td>\n" ;
echo "<td width=\"10%\" class=\"forumTable\" align=\"center\">$Year</td>\n
" ;
echo "<td width=\"50%\" class=\"forumTableRight\">$Title</td>\n" ;
echo "</tr>\n" ;

Cheers,

- Tatlar
tatlar is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 05-14-2003, 03:10 PM
  #3
torrent
Super Deity (Level 18)
 
torrent's Avatar
 
Join Date: Dec 2001
Location: UK
Posts: 2,383
iTrader: (0)
torrent is on a distinguished road
It's not screwing the query up, it's doing exactly what you've asked it to. That is to pull back a row from the result set.

Therefore, if you had 4 rows returned, then that statement pulls back the first one and leaves you with three. You have done the right thing to remove the statement from there as it didn't appear to be contributing to anything you were doing further in the code.

Btw, you don't have to do mysql_fetch_* before you do a mysql_num_rows(). Just in case that was what you thought.

Anyhoo, glad you got it working.
torrent is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 11:48 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.