doufuxing8691 2015-11-01 12:44
浏览 551
已采纳

查询无效:SQL语法中存在错误; 语法在附近使用

I have this problem error and I don't know how to solve it. I know what so many have issues like my issue but I can not orient.

Problem:

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM `user` WHERE `id` = 0' at line 6

Code:

<?php
function fetch_users(){
$result = mysql_query('SELECT `id` AS `id`, `username` AS `username` FROM `user`');

$users = array();

while(($row = mysql_fetch_assoc($result)) !== false){
       $users[] = $row;
   }

   return $users;

}

// fetches profile information for the given user.
function fetch_user_info($id){
   $id = (int)$id;

   $sql = "SELECT 
               `username` AS `username`,
               `firstname` AS `firstname`,
               `lastname` AS `lastname`,
               `email` AS `email`,
             FROM `user` WHERE `id` = {$id}";

      $result = mysql_query($sql);
      if (!$result) {
   die('Invalid query: ' . mysql_error());
   }

      return mysql_fetch_assoc($result);
}


?>
  • 写回答

2条回答 默认 最新

  • duanbei8904 2015-11-01 12:45
    关注

    Remove comma after last column:

    $sql = "SELECT 
                 `username` AS `username`,
                 `firstname` AS `firstname`,
                 `lastname` AS `lastname`,
                 `email` AS `email`              -- here
             FROM `user` WHERE `id` = {$id}";
    

    Also you don't need to alias the same name as column:

    $sql = "SELECT 
                 `username`,
                 `firstname`,
                 `lastname`,
                 `email`  
             FROM `user` WHERE `id` = {$id}";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站