gpt4 book ai didi

PHP SQL 数据库查询错误信息

转载 作者:搜寻专家 更新时间:2023-10-30 19:56:18 24 4
gpt4 key购买 nike

这段SQL代码有什么问题吗?我从教程中得到它,但它返回以下错误消息

Database query failed: 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 'LIMIT 1' at line 1

function get_subject_by_id($subject_id) {
global $connection;
$query = "SELECT * ";
$query .= "FROM subjects ";
$query .= "WHERE id=" . $subject_id ." ";
$query .= "LIMIT 1";
$result_set = mysql_query($query, $connection);
confirm_query($result_set);
// if no rows are returned, fetch array will return false
if ($subject = mysql_fetch_array($result_set)){
return $subject;
} else {
return NULL;

}
}

最佳答案

最好回显查询并查看它的样子。

可能 $subject_id 不包含值或包含无效值。如果 $subject_id 是一个字符串,您应该将其转义(使用 mysql_real_escape_string)在查询中将其放在引号内。

[编辑]

您知道您也可以在字符串中输入 enter,对吗?

// More readable
$query = "
SELECT *
FROM subjects
WHERE id = $subject_id
LIMIT 1";

关于PHP SQL 数据库查询错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5039561/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com