Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO无法在类中绑定参数_Php_Class_Pdo - Fatal编程技术网

PHP PDO无法在类中绑定参数

PHP PDO无法在类中绑定参数,php,class,pdo,Php,Class,Pdo,我无法让我的代码将参数绑定到Mysql查询,我在这里做错了什么 错误 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[HY093]:参数编号无效:绑定变量的数量与/Users/warren/Sites/Frame-Work/Modules/User-Login/classes/db.php中的标记数量不匹配。php:34堆栈跟踪:#0/Users/warren/Sites/Frame-Work/Modules/User-Login/classes/db.php(34)

我无法让我的代码将参数绑定到Mysql查询,我在这里做错了什么

错误

致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[HY093]:参数编号无效:绑定变量的数量与/Users/warren/Sites/Frame-Work/Modules/User-Login/classes/db.php中的标记数量不匹配。php:34堆栈跟踪:#0/Users/warren/Sites/Frame-Work/Modules/User-Login/classes/db.php(34):PDOStatement->execute()#1/Users/warren/Sites/framework/Modules/User Login/classes/db.php(48):第34行的dbConnect->get_contents()#2{main}抛出/Users/warren/Sites/framework/Modules/User Login/classes/db.php

代码

<?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();
?>

不能绑定表格,请使用以下方法:

$this->connect->prepare('SELECT * FROM ' . $this->select_table);

另外请注意,您使用的表应该使用select_table

您在哪里看到可以将表名指定为绑定参数?只是想知道是否有教程教人们这些东西。