【发布时间】:2014-01-05 23:52:15
【问题描述】:
我在使用删除语句时遇到问题。我的一些代码是借用的,它可能是一个简单的错误,但我无法弄清楚。我还在学习,所以它可能效率不高,但我想尝试让它工作,以便在我开始改进我的代码之前有一个工作基础。
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
【问题讨论】:
-
不,
print_r不好。它应该打印出一个对象,而不是一个字符串。再说一次,它与您的问题无关。按照其他人说的做(关于executevsexecute()),应该没问题。 -
好像是这个错误
$this->execute;添加这个$this->execute(); -
“我的一些代码是借用的,这可能是一个简单的错误,但我无法弄清楚。我还在学习,所以它可能效率不高,但我想尝试让它工作,以便在我开始进一步完善我的代码之前有一个工作基础。”所以你基本上不知道这意味着什么?好伤心。你刷代码,不知道如何适应,现在你想让我们做什么?
-
感谢您的有用回复,我阅读了一些提供示例的文章,我使用这些示例构建了一个迷你项目,并且在切换到学习 PDO 后遇到了困难。我喜欢通过选择一个我觉得有趣的项目来学习,然后尝试让它发挥作用,然后我边走边学,它并不完美,我可能应该阅读更多内容,以便更好地理解代码,但这对我有用,谢天谢地这里的真诚的人愿意一路帮助我,这个网站一直是一个巨大的帮助,如果我的问题让你冒犯了杰克,我很抱歉。
-
Christian,你能扩展一下 print_r。自从我开始学习 PDO 以来,我一直在努力寻找一种简单的方法来解决 MYSQL 语句的错误,我发现这种方法有效,如果有另一种更好的方法,如果你能分享,我会非常感激