我无法使用我的代码将params绑定到Mysql Query,我在这里做错了什么?
错误
致命错误:未捕获异常'PDOException',消息'SQLSTATE [HY093]:参数号无效:绑定变量数与/ Users / warren / Sites / Frame Work / Modules / User Login /中的标记数不匹配classes / db.php:34堆栈跟踪:#0 / Users / warren / Sites / Frame Work / Modules / User Login / classes / db.php(34):PDOStatement-> execute()#1 / Users / warren /站点/框架工作/模块/用户登录/类/ db.php(48):dbConnect-> get_contents()#main {main}抛出/ Users / warren / Sites / Frame Work / Modules / User Login / classes /第34行的db.php
CODE
<?php
include '../../../../config.php';
$dbUser = $config['username'];
$dbPass = $config['password'];
class dbConnect {
public function connect($dbUser,$dbPass) {
$this->dbUser = $dbUser;
$this->dbPass = $dbPass;
try {
$this->connect = new PDO('mysql:hostname=localhost;dbname=totalrisk', $this->dbUser, $this->dbPass);
$this->connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->connect;
} catch (Exception $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
public function select_table($table) {
$this->select_table = $table;
echo $this->select_table;
}
public function select_col($col) {
$this->select_col = $col;
echo $this->select_col;
}
public function get_contents() {
$stmt = $this->connect->prepare('SELECT * FROM :table');
$stmt->bindParam(':table', $this->table , PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetchAll();
while($row = $stmt->fetch()) {
print_r($row);
}
}
}
$conn = new dbConnect();
$conn->connect($dbUser,$dbPass);
$conn->select_table('users');
$conn->select_col('site_name');
$conn->get_contents();
?>
答案 0 :(得分:1)
表格不能绑定,请使用:
$this->connect->prepare('SELECT * FROM ' . $this->select_table);
另请注意,您使用表格,您应该使用select_table