Php 无效查询:SQL语法中有错误;使用near的语法

Php 无效查询:SQL语法中有错误;使用near的语法,php,mysql,sql,syntax,executequery,Php,Mysql,Sql,Syntax,Executequery,我有这个问题,我不知道如何解决它。我知道有这么多的问题,像我的问题,但我不能定向 问题: 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 代码: 删除最后一列后的逗号:

我有这个问题,我不知道如何解决它。我知道有这么多的问题,像我的问题,但我不能定向

问题:

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
代码:


删除最后一列后的逗号:

$sql = "SELECT 
             `username` AS `username`,
             `firstname` AS `firstname`,
             `lastname` AS `lastname`,
             `email` AS `email`              -- here
         FROM `user` WHERE `id` = {$id}";
此外,您不需要与列别名相同:

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

问题在于
$sql
中sql查询的语法形式,因为错误本身告诉我们该错误在id=0的用户的
附近,所以
选择
查询中的
电子邮件附近的附加
逗号,
会抛出sql错误

<?php
echo 'test';

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);
}
echo fetch_users();

?>

非常感谢您
<?php
echo 'test';

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);
}
echo fetch_users();

?>