我有一个php文件,它充当程序和我的数据库之间的抽象。我试着让它返回一个值,我收到了这个错误:
ERROR: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ': USERNAME
ORDER BY id DESC
LIMIT 1' at line 2
我的API的整个代码是:
<?php
header("Content-Type: application/json; charset=UTF-8");
require 'UHCauth.php';
try {
$conn = new PDO("mysql:host=$mysql_serv;dbname=$mysql_db",
$mysql_user, $mysql_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if(isset($_GET['USERNAME'])) {
$USERNAME = $_GET['USERNAME'];
$stmt = $conn->prepare(
"SELECT * FROM $mysql_table
WHERE USERNAME = : USERNAME
ORDER BY id DESC
LIMIT 1"
);
$stmt->execute(array('USERNAME' => $USERNAME));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
} else {
$stmt = $conn->prepare(
"SELECT * FROM $mysql_table
ORDER BY
McVersion DESC,
ModVersion DESC
LIMIT 1"
);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo json_encode($row);
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
应该使用用户名并以JSON格式返回该用户名的行。这段代码以前用于不同的项目,现在决定不工作。
答案 0 :(得分:2)
更改此
WHERE USERNAME = : USERNAME
^--//dont make sapce here
到
WHERE USERNAME = :USERNAME