drn61317 2014-03-30 01:02
浏览 83
已采纳

尝试以ASC顺序获取结果但收到错误

I have this code (refer below) to display result from mysql in ASC order

function showMenu(){
global $con; // SIMPLY ADD THIS LINE
$result = mysqli_query($con,"SELECT * FROM user_menu ORDER BY key ASC");
$menu = '<ul class="nav navbar-nav">';
while($row = mysqli_fetch_array($result))
{
$menu .= '<li class="' . $row['status'] . '"><a href="' . $row['url'] . '" class="extend"     title="' . $row['url'] . '">' . $row['name'] . '</a></li>';
}
$menu .= '</ul>';

return $menu;
}

but i got an error saying " Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in" and it point to line 38 and the line 38 is "while($row = mysqli_fetch_array($result))"

as you can see im trying to fetch result in ASC order by key and the key row contains numbers like, 0 1 2 3 4 so the result should be displayed as per number order.

how can i get raid of this error? ideas, suggestion and recommendations please let me know. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duankui1532 2014-03-30 01:08
    关注

    Key is a reserved word in MySQL. You can see the list here.

    That means that you need to escape it:

    $result = mysqli_query($con,"SELECT * FROM user_menu ORDER BY `key` ASC");
    

    If you are in control of the database, you should change the name of the column. Avoid using reserved words as identifiers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)