I’m just dabbling with PDO for the first time, and I keep getting an error that I really can’t figure out, the code causing the problem is as follows:
<?php
try {
// Querying the database
$STH = $DBH->query("SELECT 'Group' from 'GroupTable'");
// Setting the fetch mode
$STH->setFetchMode(PDO::FETCH_ASSOC);
while($row = $STH->fetch()) {
echo '<option value="',$row['Group'],'">',$row['Group'],'</option>';
}
}
catch(PDOException $e) {
echo "Error connecting to Database";
file_put_contents('logs/logsDB.txt', $e->getMessage(), FILE_APPEND);
}
?>
I keep getting the same error, namely:
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 ”GroupTable”
at line 1
I’ve tried quoting/unquoting the table/column name, when unquoted I get this 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 ‘Group FROM
GroupTable’ at line 1
I’d checked and double checked, the Table is DEFINITELY called GroupTable and the column is definitely called Group.
I’m sorry if this is something really obvious or nooby, I’m just trying to mess around with this and see if I can learn the basics, apologies if the code doesn’t conform to standards etc.
Two problems here:
GROUPis a reserved word in mySQL, so it’ll work only when quoted.Table and column names need to be quoted using backticks, not simple quotes: