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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Missing first record in PHP / MySQL page

Status
Not open for further replies.

crighton

Programmer
Aug 31, 2001
7
ES
Hi,

I'm not sure if the following is a MySQL or PHP issue but would appreciate all advice.

I have a page where a list of names are to be displayed. I present the user the option of choosing the sort order using links of the letters A-Z but also the option to show all names on the same page.

I thought it was working fine but have just realised that in each case the very first record is missing from any choice. So, for example if the user clicks "A" the first name displayed should be Adami, Valerio followed by Adela, R.D. etc. In fact the first name is skipped and all names from the second onwards print fine. This happens with all choices A-Z and with the show all option (in this case Adami is the only record skipped as he is the first).

The page can be viewed here (with code extracts included below):


Can anyone can shed any light as to where this error is being caused and how it might be fixed?

Many thanks,

Gary Crighton

CODE EXTRACTS:

My sort links takes the following form:

<a href=&quot;<?PHP $PHP_SELF ?>?lang=<?PHP echo $lang ?>&sort=a&quot;>A</a>


My select statement:

if ($sort == &quot;all&quot;) {
$result = mysql_query (&quot;SELECT * FROM biografias WHERE Tipo = 1 ORDER BY nombre&quot;);
} else {
$result = mysql_query (&quot;SELECT * FROM biografias WHERE Tipo = 1 AND LOWER(SUBSTRING(nombre,1,1)) = '$sort' ORDER BY nombre&quot;);
}


Printout statements as follows:

<?
if ($row = mysql_fetch_array($result) == &quot;&quot;) {
?>

Etc...

<?
} else {
while ($row = mysql_fetch_array($result)) {
?>

<tr><td class='list'>
<a href='/production/artistas/artistas_detalle.php?lang=<?PHP echo $lang ?>&ID=<?PHP echo $row[&quot;ID&quot;]?>'><?PHP echo $row[&quot;Nombre&quot;]?></a>
</td></tr>

?>

Etc...
 
as you have not posted all the code, i think your problem is caused because you first check if there's some data:
Code:
if ($row = mysql_fetch_array($result) == &quot;&quot;)
and if yes, then you do a while loop on the query result
but when you do a mysql_fetch_array, the pointer to the current record is moved to the following record
e.g.
$result = &quot;first record&quot;, &quot;second record&quot;, &quot;third record&quot;
now the pointer is set on &quot;first record&quot;
so when you issue in the
Code:
if
statement the mysql_fetch_array() function, the pointer moves to &quot;second record&quot; and therefore you don't have you first returned record echoed on the screen
i suggest you to do it as follows:
1.mysql_query
2.while(@row=mysql_fetch_array) {}
this way you will not &quot;loose&quot; Adami, Valerio or any other record returned

hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top