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

Determining if the result is the first.

Status
Not open for further replies.

pugs421

Technical User
Joined
Nov 25, 2002
Messages
114
Location
US
How do I determine if the result from a query is the first out of all of the results?
 
It depends on how you fetch the records.

Generally, I use a while loop. If I need to know that a record is first, I set a variable named "$first_record" to TRUE just before the beginning of the while loop. At the end of the loop, I set it to FALSE.

Inside the loop, I can perform branching based on $first_record.

Want the best answers? Ask the best questions: TANSTAAFL!!
 
if ($count == 1)
$column1 = col1whatever;
$column2 = col2whatever;
and so on
put it in ur loop which is getting query

To err is human, to completely mess up takes a computer. [morning]
 
Sorry guys, Im still a newb.

"It depends on how you fetch the records."

I guess this is how.

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
 
Actually, the code you've posted hasn't fetched any records at all. The record retrieval doesn't take place until you invoke a function like mysql_fetch_array():

$query=" SELECT * FROM contacts WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);

$first_row = TRUE;
while ($therow = mysql_fetch_array($result))
{
.
.
.
$first_row = FALSE;
}

Want the best answers? Ask the best questions: TANSTAAFL!!
 
The question is still not clear.
Is it:
1. How can I tell when a loop runs for the first time?
Yes? Then, do what sleipnir214 told you above, use a flag.
or:
2. How can I make sure that I retrieve the first row that matches my SQL condition?
Different problem - it can be handled in SQL using the "LIMIT 1" construct.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top