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!

Two results from one query 1

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB

Hi

Is it possible to have two result rows from one query??

Here's a brief example...

I have a query such as this
Code:
$nav_result = mysql_query("select * from navigation where main='about' order by sort ASC");
$meta_row = mysql_fetch_row($nav_result);
In my page HEAD I have this:
Code:
<title><?php echo $meta_row[5]; ?></title>
<meta name='description' content='<?php echo $meta_row[6];?>'>
<meta name='keywords' content='<?php echo $meta_row[7];?>'>
Everything works fine up to this point.

Further down in my page I have a while loop which builds a 2nd level navigation...
Code:
while ($nav_row = mysql_fetch_array($nav_result, MYSQL_NUM)) {
echo '<a href='.$nav_row[4] .'>'.$nav_row[2].'</a><br />';
}

The problem I get with this is that the first entry of the navigation (the first entry in the database table) is missing.
Any reason from the above code??
 
sure. just add the following line before restarting the loop

Code:
mysql_data_seek($nav_result, 0);
 
or how about:
Code:
$result = mysql_query("select * from navigation where main='about' order by sort ASC");
$row = mysql_fetch_array($result, MYSQL_NUM);

<title><?php echo $row[5]; ?></title>
<meta name='description' content='<?php echo $row[6];?>'>
<meta name='keywords' content='<?php echo $row[7];?>'>

while($row)
     {echo '<a href='.$row[4] .'>'.$row[2].'</a><br />';
      $row = mysql_fetch_array($result, MYSQL_NUM);
     }

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
jpadie - that did the trick.

And I understand how this works. I assume my first result uses the first row in the variable whilst the second result starts at the next row in variable. Therefore I lose the first entry?
Correct?

 
yes. it's like an array - every time you call mysql_fetch_* you iterate the counter on the recordset. the mysql_data_seek() used as above, resets the counter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top