php - PDO syntax error for SELECT query

108

I have a basic query which gets all fields and exports the file but it keeps giving me an error. My code looks like this:

$array = ['users'];

foreach($array AS $i){
        $file = $i.'.sql';

        $stmt = $pdo->prepare("SELECT * FROM ? INTO OUTFILE ?");
        try {
            $stmt->execute(array($i,$file));
        } catch (PDOException $e) {
            $log .= $e -> getMessage().'........ \n ';
        }
}

I keep getting this error how ever:

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 ''users' INTO OUTFILE 'users.sql'' at line 1.

What is the correct syntax for this ?

503

Answer

Solution:

It(Syntax) should be the other way around

SELECT * INTO OUTFILE ? FROM ?;

See the documentation

EDIT :

As JAL clarified, The table name cannot be passed as a parameter under a PreparedStatement. So your query should be like

SELECT * INTO OUTFILE ? FROM users;
832

Answer

Solution:

You cannot prepare a statement where the table name is a parameter.

See Can PHP PDO Statements accept the table or column name as parameter?

People are also looking for solutions to the problem: php - Shortcode using WP_User object

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.