Dynamic table name setting with PDO

Liably

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);
Your Common Sense

Do not confuse PHP and SQL.
You just used SQL quotes in PHP. While you have to use SQL quotes in SQL.

Linked answer is now fixed, so, I am closing this as a duplicate.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Setting href to dynamic view name

From Dev

dynamic table name in cursor

From Dev

Dynamic table name in linq

From Dev

Setting a dynamic sort name field in a linq query

From Dev

Unable to quote table name in PDO with MSSQL

From Dev

How to do parametrize the table name in a PDO statement?

From Dev

Unable to quote table name in PDO with MSSQL

From Dev

PDO query with variable and string concatenated as table name

From Dev

Using variable as table name in SQL pdo query

From Dev

Dynamic table name at sql statement

From Dev

Dynamic Table Name in Propel Query

From Dev

UPDATE TABLE with dynamic COLUMN name

From Dev

Dynamic Table Name in Propel Query

From Dev

Mysql server and dynamic table name

From Dev

Dynamic SQL with table name as a parameter

From Dev

Run a query on Dynamic Table Name

From Dev

jOOQ table template, dynamic table name for queries

From Dev

Dynamic table name override in Slick table

From Dev

How to update table with dynamic table name?

From Dev

Is there a way to use PDO with a dynamic column name in an UPDATE query?

From Dev

Is there a way to use PDO with a dynamic column name in an UPDATE query?

From Dev

PDO Insert into database values from dynamic html table

From Dev

BIRT : Data set with dynamic table name

From Dev

How to set table name in dynamic SQL query?

From Dev

plpgsql - using dynamic table name in declare statement

From Dev

Filter data table by dynamic column name

From Dev

MySQL dynamic SQL table name and result

From Dev

create a dynamic table name from current year

From Dev

Using myBatis with dynamic table name and object