PHP PDO致命错误 - 绑定

时间:2017-04-02 22:23:55

标签: php pdo

我写了一些像这样的数据库代码:

  

 $db = new Database();
    $call_number = '123456';
    $sql = "SELECT * FROM nfirs_export WHERE incident_number = :callnumber";
    $db->bind(':callnumber', $call_number);

    $db->query($sql);
    $row = $db->single();

我在调用$ db-> bind的行上遇到致命错误。

我的数据库类包装PDO如下:

  

private $host      = '##########.com'; 
private $user      = '########';
private $pass      = '########';
private $dbname    = '########';

private $dbh;
private $error;
private $stmt;

public function __construct(){
    // Set DSN
    $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
    // Set options
    $options = array(
        PDO::ATTR_PERSISTENT    => true,
        PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
    );
    // Create a new PDO instanace
    try{
        $this->dbh = new PDO($dsn, $this->user, $this->pass); //, $options);
    }
    // Catch any errors
    catch(PDOException $e){
        $this->error = $e->getMessage();
    }
}


public function query($query){
$this->stmt = $this->dbh->prepare($query);
}

//In order to prepare our SQL queries, we need to bind the inputs with the placeholders we put in place
public function bind($param, $value, $type = null){
if (is_null($type)) {
    switch (true) {
        case is_int($value):
            $type = PDO::PARAM_INT;
            break;
        case is_bool($value):
            $type = PDO::PARAM_BOOL;
            break;
        case is_null($value):
            $type = PDO::PARAM_NULL;
            break;
        default:
            $type = PDO::PARAM_STR;
    }
}
$this->stmt->bindValue($param, $value, $type);
}

//The execute method executes the prepared statement.
public function execute(){
return $this->stmt->execute();
}

//The Result Set function returns an array of the result set rows.
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

//Single method simply returns a single record from the database
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

//returns the number of effected rows from the previous delete, update or insert statement
public function rowCount(){
return $this->stmt->rowCount();
}

//Last Insert Id method returns the last inserted Id as a string
public function lastInsertId(){
return $this->dbh->lastInsertId();
}


//Transactions allows you to run multiple changes to a database all in one batch to ensure that your work will not be accessed 
//incorrectly or there will be no outside interferences before you are finished. If you are running many queries that all rely upon each other, 
//if one fails an exception will be thrown and you can roll back any previous changes to the start of the transaction.
public function beginTransaction(){
return $this->dbh->beginTransaction();
}

public function endTransaction(){
return $this->dbh->commit();
}

public function cancelTransaction(){
return $this->dbh->rollBack();
}


}

此数据库代码用于网站中的100多个其他类似查询,并且工作正常。我无法弄清楚为什么会出现这个错误。我已经尝试过硬编码值来绑定,传递变量,输出信息到控制台,我无法跟踪问题。 如果你能提供帮助,请告诉我。

0 个答案:

没有答案