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

First Field isn't read using SELECT

Status
Not open for further replies.

CliffLandin

Programmer
Nov 14, 2004
81
US
I am trying to gather information from a dbase, displaying it as I go. The script works fine except the first field that matches the criteria isn't read or displayed.

For example:

On this page I check to see if a session variable 'tripNumb' is set. If it isn't, it goes to the dbase and gets the last trip number and adds one to it. The problem is that if there is only one field in the dbase it doesn't read it.

This is how I am reading the fields:

{
$link = mysql_connect(*,*,*);
if (!$link) {
die("Could not connect: " . mysql_error());
}
mysql_select_db(*, $link)
or die("Could not select database");

$query = "SELECT nID, tripNO FROM reqsTL";
$result = mysql_query($query);
$row = mysql_fetch_row($result);

while ($row = mysql_fetch_row($result)){
$nID=$row[0];
$tripNO=$row[1];
}

mysql_close($link);
$tripNO++;
$_SESSION['tripNum']=$tripNO;

}

If there was one field in the dbase and the trip number for that field is 150 the query above would return 0 and add 1 to it. If there were two fields in the dbase and the trip number for the second field was 151 the query above would return 151 and add 1 to it so $tripNO and $_SESSION['tripNum'] would be 152.

This occurs in a couple of different spots in the page. In fact, whereever I use the while ($row = mysql_fetch_row($result)).

What am I doing wrong? It is really infuriating.

Thanks for any help that anyone can offer.

When in doubt, go flat out!

 
Hi,
Try this:
Code:
{
$link = mysql_connect(*,*,*);
if (!$link) {
   die("Could not connect: " . mysql_error());
}
mysql_select_db(*, $link)
 or die("Could not select database");

$query = "SELECT nID, tripNO FROM reqsTL";
$result = mysql_query($query);
$row = mysql_fetch_row($result);

while ($row = mysql_fetch_row($result)){
$nID=$row['nID'];
$tripNO=$row['tripNO'];
}

mysql_close($link);
$tripNO++;
$_SESSION['tripNum']=$tripNO;

}

[cheers]
Cheers!
Laura
 
hi

I think I can see your problem.

line 1 $row = mysql_fetch_row($result);

line 2 while ($row = mysql_fetch_row($result)){
line 3 $nID=$row[0];
line 4 $tripNO=$row[1];
line 5 }

you access the results with line one, which moves the results cursor on one position, to the second record found, then in line 2 you call the same function again which will read the second row of results and move the cursor on one position.

Try just deleting line 1.

Let me know if that works.

Looking for a job as a programmer in Bristol UK.
 
Glad to help :)

The results returned from an sql query are stored in a structure of some form (hash array, or list etc depending on language) when you call the mysql_fetch_row() function if advances the pointer/cursor of the result set by one position. If you call it twice and do nothing with the row data, the cursor still moves on.

Looking for a job as a programmer in Bristol UK.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top