php - 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 ?
Answer
Solution:
It(Syntax) should be the other way around
See the documentation
EDIT :
As JAL clarified, The table name cannot be passed as a parameter under a
PreparedStatement
. So your query should be likeAnswer
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?