Sir
Sir

Reputation: 8280

PDO syntax error for SELECT query

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 ?

Upvotes: 2

Views: 791

Answers (2)

It(Syntax) should be the other way around

SELECT * INTO OUTFILE ? FROM ?;

See the documentation here

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;

Upvotes: 2

JAL
JAL

Reputation: 21563

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?

Upvotes: 1

Related Questions