作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这个问题错误,我不知道如何解决。我知道有多少人有像我这样的问题,但我无法定位。
问题:
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
代码:
<?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);
}
?>
最佳答案
删除最后一列后的逗号:
$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}";
关于php - 无效查询 : You have an error in your SQL syntax; syntax to use near,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33462270/
我是一名优秀的程序员,十分优秀!