9951 explained code solutions for 126 technologies


php-pdoHow to bind table name in PHP PDO


You can't bind table names with PDO, but you can use white filtering and direct string insertion:

$allowed = ['test', 'users', 'messages'];
$table = 'test';

if ( in_array($table, $allowed) ) {
  $st = $pdo->prepare("SELECT * FROM {$table}");
  $st->execute();
  // ...
}ctrl + c
$allowed

list of allowed table names (it is important to use this white filter for safety)

$table

table name we want to "bind"

$pdo->prepare

prepare given query to execute

$st->execute(

run query on the server