viktorino
viktorino

Reputation: 809

Delete From table with PDO Statement

hey guys sory but my English its not very good . i got a problem with execute delete table method. when im doing var_dump() to query its given me a right query like this :

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

i checked all steps thet before execute and all steps are returning me the right query. and when im doing execute i got error like this :

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

this is my code :

            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);
                        }
                  }
           }

and here i run this method :

     require_once 'autoload.php';

     $db = new \DBWork\DBWork();

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

Upvotes: 0

Views: 302

Answers (1)

Peter van der Wal
Peter van der Wal

Reputation: 11796

The error is pretty self-explaining: the table doesn't exists. The format used is as follow:

database-name.table-name

So your current database (use a query USE databaseName to select a database) is 'test', and the table you are trying to delete is 'test', hence the notation 'test.test'

Upvotes: 1

Related Questions