从具有PDO语句的表中删除

时间:2013-12-07 21:21:00

标签: php pdo

嘿伙计们,但我的英语不太好。我遇到了执行删除表方法的问题。 当我正在做var_dump()来查询给我一个正确的查询时:

 public 'queryString' => string 'DELETE FROM `test` WHERE  `id` = ? LIMIT 1' .

我在执行前检查了所有步骤,所有步骤都返回了正确的查询。 当我做执行时,我得到这样的错误:

 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.test' doesn't exist

这是我的代码:                 

            use PDO;
            use PDOException;

            class Delete extends Query
            {
                 public $where = array();
                 public $params = array();
                 public $limit = 1;

                 private function fieldClause($field)
                 {
                      return $filed = "`$field`";
                 }

                 public function where($field, $operation, $value)
                 {
                 $field = $this->fieldClause($field);
                 $this->where = " $field $operation ?";
                 $this->params[] = $value;
                 return $this;
                 }

                 public function limit($limit)
                 {
                 $this->limit = $limit;
                 return $this;
                 }

                 private function buildSql()
                 {
                 $sql = "DELETE FROM $this->table";
                 $sql .= " WHERE $this->where";
                 $sql .= " LIMIT $this->limit";
                 var_dump($sql);
                 return $sql;
                 }

                 protected function runSQL($debug = false)
                 {
                      $sql = $this->buildSql();

                      if ($debug) {
                      Log::query($sql, $this->params);
                      }

                      $this->dbh->beginTransaction();
                      try {

                      $query = $this->dbh->prepare($sql);
                      var_dump($this->params);

                      foreach ($this->params as $key => $val) {

                      $type = is_null($val) ? PDO::PARAM_NUL : PDO::PARAM_STR;
                      $type = is_bool($val) ? PDO::PARAM_BOOL : PDO::PARAM_STR;
                      $type = is_integer($val) ? PDO::PARAM_INT : PDO::PARAM_STR;

                      $query->bindValue($key+1, $val, $type);
                      var_dump($query);
                      }

                      $query->execute();


                      return $query;

                      } catch (PDOException $e) {
                            $this->dbh->rollBack();
                            Log::error($e);
                        }
                  }
           }

在这里我运行这个方法:

     require_once 'autoload.php';

     $db = new \DBWork\DBWork();

     $del = $db->delete('test')->where('id', '=', 3);
     $del->deleteOne(true);

1 个答案:

答案 0 :(得分:1)

该错误非常自我解释:该表不存在。 使用的格式如下:

database-name.table-name

因此,您当前的数据库(使用查询USE databaseName选择数据库)是'test',您尝试删除的表是'test',因此表示'test.test'