$dsn = 'mysql:host=127.0.0.1;dbname=test';
$username = 'root';
$password = 'root';
try {
$pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo $e->getMessage();
exit;
}
$sql = 'SELECT * FROM :table_name'; // 命名参数
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':table_name', $table_name, PDO::PARAM_STR); // 绑定到$table_name 变量
$table_name = 'yd_user'; // 给变量赋值
$stmt->execute();
print_r($stmt->errorInfo());
// print_r($stmt->fetchAll());
// --------------------------
// 输出错误信息:
Array
(
[0] => 42000
[1] => 1064
[2] => 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 ''yd_user'' at line 1
)