So I am quite new to using PDO and although I have to it to work using static settings I would like to be able to change the table name in a statement.
I had a look at this question here. and saw the highly up voted answer. So I attempted to recreate a simple version which would just dump all the information within a column.
Question: Why is my edited version of that persons example not work and return this error below.
Fatal error: Uncaught exception 'PDOException' with message '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 '' at line 1' in C:\xampp\htdocs\ *****\database.php:84 Stack trace: #0 C:\xampp\htdocs\ *****\database.php(84): PDOStatement->execute() #1 C:\xampp\htdocs\ *****\database.php(88): buildQuery(1) #2 {main} thrown in C:\xampp\htdocs\ *****\database.php on line 84
The code from the example linked with my small alterations.
function buildQuery( $get_var )
{
switch($get_var)
{
case 1:
$tbl = `r16.7`;
break;
}
global $db;
$sql = "SELECT * FROM $tbl";
$stmt = $db->prepare($sql);
$stmt->execute();
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 1);
var_dump($result);
}
buildQuery(1);