Php pdo中的参数错误
我有一个php文件,作为程序和数据库之间的抽象。我试图让它返回一个值,但出现以下错误:Php pdo中的参数错误,php,mysql,database,api,Php,Mysql,Database,Api,我有一个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 ': USERNA
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格式返回该用户名的行。此代码以前适用于其他项目,现在决定不使用。更改此代码
WHERE USERNAME = : USERNAME
^--//dont make sapce here
到
是的。这解决了我的问题。不知道那空间是怎么潜入的。
WHERE USERNAME = :USERNAME