简体   繁体   中英

SQL query not displaying the first result?

I am trying to dynamically create a navigation menu in my php page.

I have a query to create a list of the active pages but for some reason the first result never shows

$menu = mysql_query("SELECT link FROM myTable WHERE active_page='y' ORDER BY menu_order");
$menulist = mysql_fetch_array($menu);

  while($menulist = mysql_fetch_array($menu))
  {
  $themenu = $themenu . "<li><a href='#'>" . $menulist['link'] . "</a></li>";
  }

  $echo $themenu;

returns

item 2
item 3
item 4
...

Any ideas why this might be

Remove

$menulist = mysql_fetch_array($menu);

This extraneous call is pulling the first record, but you're not doing anything with it as you are in your where loop.

It doesn't display the first item because you are fetching twice before displaying anything...

$menu = mysql_query("SELECT link FROM myTable WHERE active_page='y' ORDER BY menu_order");

  while($menulist = mysql_fetch_array($menu))
  {
  $themenu = $themenu . "<li><a href='#'>" . $menulist['link'] . "</a></li>";
  }

  $echo $themenu;

You should drop

$menulist = mysql_fetch_array($menu);

This moves the cursor one step forward and efficently skips the first row.

You should also drop the $ before your echo .

它正在跳过第一个元素,因为你在while循环之前调用了mysql_fetch_array($menu)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM