SQL query not displaying the first result?

╄→гoц情女王★ 提交于 2019-12-07 19:51:31

问题


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


回答1:


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.




回答2:


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;



回答3:


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.




回答4:


It's skipping the 1st element because you are calling mysql_fetch_array($menu) before the while loop.



来源:https://stackoverflow.com/questions/5492344/sql-query-not-displaying-the-first-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!