PDO删除声明问题

时间:2014-01-05 23:52:16

标签: php mysql pdo

我在删除语句时遇到问题。我的一些代码是借用的,它可能是一个简单的错误,但我无法弄明白。我还在学习,所以它可能效率不高,但是我想尝试让它在我开始完善我的代码之前有一个工作基础。

public function delete_data($id,$table){

    $sql = 'DELETE FROM `'.$table.'` WHERE `my_id` = :id';
    $this->prepare_qry($sql);
    $this->bind(':id',$id);
    $this->execute;

    $statement = print_r($this->stmt,true);
    echo "statement:" . $statement;
}

当我回应这个时,我得到:

DELETE FROM `my_table` WHERE `my_id` = :id

所以看起来很好。以下是一些相关功能

// Create a new PDO instanace
try{
    $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    } catch(PDOException $e){ // Catch any errors
    $this->error = $e->getMessage();
}

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

// add the type for the binding
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;
}
}
// run the binding process
$this->stmt->bindValue($param, $value, $type);
}

// execute query
public function execute(){
    return $this->stmt->execute();
}

运行代码时出现以下错误。任何想法是什么问题:

Undefined property: Database::$execute

1 个答案:

答案 0 :(得分:2)

您已撰写->execute,而不是->execute()

->execute正在尝试访问某个媒体资源。 ->execute()正在调用该函数。