Element
2015-10-27 14:26
$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 )
其实你的SQL语句是select * from 'yd_user';
$sql = 'SELECT * FROM :table_name'; // 命名参数
修改成
$sql = "SELECT * FROM :table_name"; // 命名参数
PDO—数据库抽象层
30043 学习 · 396 问题
相似问题