I have a php file that acts as an abstraction between a program and my database. I tried getting it to return a value and I got this error:
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
The entire code for my API is this:
<?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();
}
?>
It is supposed to take a username and return the row for that username in JSON format. This code has worked before for a different project and now it's decided not to work.
change this
WHERE USERNAME = : USERNAME
^--//dont make sapce here
to
WHERE USERNAME = :USERNAME