Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Results Display skipping 1st row - mySQL

Status
Not open for further replies.

danielleDee

Programmer
Jul 29, 2004
5
ZA
Hi!

When I display the entire results from a database table (mySQL), for some reason the 1st line is left out. My code is:

<?php

include_once ("dbc.php");
$sql=@mysql_query ("SELECT * FROM `my_desc`");
$row=mysql_fetch_array($sql);
$num_rows=mysql_num_rows($sql);
if ($num_rows < 0)
{
echo "No Results Available";
}
while ($display = mysql_fetch_array($sql)) {
$id= $display["id"];
$title= $display["title"];
$status= $display["status"];

echo($id);
echo($title);
echo($status);
}
?>

My primary key is set as auto_increment on id.

If there is 3 rows:
1, Title 1, sold
2, Title 2, sold
3, Title 3, bid

then only the rows for Title 2 and Title 3 will display.

Any ideas?

Thanks!
 
You have a $row = mysql_fetch_array($sql) line where it shouldn't be. This is, in essence, paging into the array once. So, when you enter your loop, you're going right to the second record.

Comment out or remove the following line:
[tt]$row=mysql_fetch_array($sql);
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Another suggestion:
Code:
$sql=@mysql_query ("SELECT * FROM `my_desc`");
The @ operator will suppress error messages, the query however might still fail. In terms of error handling a graceful recovery strategy seems the best. At least there should be a notice.
If the query fails the script will choke next on the line where it tries to retrieve the row. It will say that there is no valid resource.
What I suggest is:
Code:
$sql = mysql_query ("SELECT * FROM `my_desc`") OR die('Query error: '.mysql_error());
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top