doufuxing8691 2015-11-01 04: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 04: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部