2015-10-22 32 views
1

我不斷收到以下錯誤:致命錯誤:調用一個成員函數的execute()上的空

Fatal error: Call to a member function execute() on null in /home/[sitename]/public_html/fc/includes/class_db_handle.php on line 130

這是從U-拍賣腳本,我真的感到非常小白到PDO 請幫助「DUMMIE條款」。

if (!defined('InuAuctions')) exit('Access denied'); 

class db_handle 

{ 

    // database 

    private  $pdo; 

    private  $DBPrefix; 

    private  $CHARSET; 

    private  $lastquery; 

    private  $fetchquery; 

    private  $error; 

    public  $PDOerror; 



    public function connect($DbHost, $DbUser, $DbPassword, $DbDatabase, $DBPrefix, $CHARSET) 

    { 

     $this->DBPrefix = $DBPrefix; 

     $this->CHARSET = $CHARSET; 

     try { 

      // MySQL with PDO_MYSQL 

      $this->pdo = new PDO("mysql:host=$DbHost;dbname=$DbDatabase;charset =$CHARSET", $DbUser, $DbPassword); 


      // set error reporting up 

      $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

      // actually use prepared statements 

      $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    // to run a direct query 

    public function direct_query($query) 

    { 

     try { 

      $this->lastquery = $this->pdo->query($query); 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    // put together the quert ready for running 

    /* 

    $query must be given like SELECT * FROM table WHERE this = :that AND where = :here 

    then $params would holds the values for :that and :here, $table would hold the vlue for :table 

    $params = array(

     array(':that', 'that value', PDO::PARAM_STR), 

     array(':here', 'here value', PDO::PARAM_INT), 

    ); 

    last value can be left blank more info http://php.net/manual/en/pdostatement.bindparam.php 

    */ 

    public function query($query, $params = array()) 

    { 

     try { 

      //$query = $this->build_query($query, $table); 

      $params = $this->build_params($params); 

      $params = $this->clean_params($query, $params); 

      $this->lastquery = $this->pdo->prepare($query); 

      //$this->lastquery->bindParam(':table', $this->DBPrefix . $table, PDO::PARAM_STR); // must always be set 

      foreach ($params as $val) 

      { 

       $this->lastquery->bindParam($val[0], $val[1], @$val[2], @$val[3], @$val[4]); 

      } 

      $this->lasta->execute(); 

      //$this->lastquery->debugDumpParams(); 

     } 

     catch(PDOException $e) { 

      //$this->lastquery->debugDumpParams(); 

      $this->trigger_error($e->getMessage()); 

     } 



     //$this->lastquery->rowCount(); // rows affected 

    } 



    // put together the quert ready for running 

    public function fetch($method = 'FETCH_ASSOC') 

    { 

     try { 

      // set fetchquery 

      if ($this->fetchquery == NULL) 

      { 

       $this->fetchquery = $this->lastquery; 

      } 

      if ($method == 'FETCH_ASSOC') $result = $this->fetchquery->fetch(PDO::FETCH_ASSOC); 

      if ($method == 'FETCH_BOTH') $result = $this->fetchquery->fetch(PDO::FETCH_BOTH); 

      if ($method == 'FETCH_NUM') $result = $this->fetchquery->fetch(PDO::FETCH_NUM); 

      // clear fetch query 

      if ($result == false) 

      { 

       $this->fetchquery = NULL; 

      } 

      return $result; 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    // put together the quert ready for running + get all results 

    public function fetchall($method = 'FETCH_ASSOC') 

    { 

     try { 

      // set fetchquery 

      if ($this->fetchquery == NULL) 

      { 

       $this->fetchquery = $this->lastquery; 

      } 

      if ($method == 'FETCH_ASSOC') $result = $this->fetchquery->fetchAll(PDO::FETCH_ASSOC); 

      if ($method == 'FETCH_BOTH') $result = $this->fetchquery->fetchAll(PDO::FETCH_BOTH); 

      if ($method == 'FETCH_NUM') $result = $this->fetchquery->fetchAll(PDO::FETCH_NUM); 

      // clear fetch query 

      if ($result == false) 

      { 

       $this->fetchquery = NULL; 

      } 

      return $result; 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    public function result($column = NULL) 

    { 

     $data = $this->lastquery->fetch(PDO::FETCH_BOTH); 

     if (empty($column) || $column == NULL) 

     { 

      return $data; 

     } 

     else 

     { 

      return $data[$column]; 

     } 

    } 



    public function numrows() 

    { 

     try { 

      return $this->lastquery->rowCount(); 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    public function lastInsertId() 

    { 

     try { 

      return $this->pdo->lastInsertId(); 

     } 

     catch(PDOException $e) { 

      $this->trigger_error($e->getMessage()); 

     } 

    } 



    private function clean_params($query, $params) 

    { 

     // find the vars set in the query 

     preg_match_all("(:[a-zA-Z_]+)", $query, $set_params); 

     //print_r("params" . $query); 

     //print_r($params); 

     //print_r("set_params"); 

     //print_r($set_params); 

     $new_params = array(); 

     foreach ($set_params[0] as $val) 

     { 

      $key = $this->find_key($params, $val); 

      $new_params[] = $params[$key]; 

     } 

     //print_r("new_params"); 

     //print_r($new_params); 

     return $new_params; 

    } 



    private function find_key($params, $val) 

    { 

     foreach ($params as $k => $v) 

     { 

      if ($v[0] == $val) 

       return $k; 

     } 

    } 



    private function build_params($params) 

    { 

     $PDO_constants = array(

      'int' => PDO::PARAM_INT, 

      'str' => PDO::PARAM_STR, 

      'bool' => PDO::PARAM_BOOL, 

      'float' => PDO::PARAM_STR 

      ); 

     // set PDO values to params 

     for ($i = 0; $i < count($params); $i++) 

     { 

      // force float 

      if ($params[$i][2] == 'float') 

      { 

       $params[$i][1] = floatval($params[$i][1]); 

      } 

      $params[$i][2] = $PDO_constants[$params[$i][2]]; 

     } 

     return $params; 

    } 



    private function trigger_error($error) 

    { 



     // DO SOMETHING 

     //$this->error = $error; 

     $this->PDOerror = $error; 

    } 



    // close everything down 

    public function __destruct() 

    { 

     // close database connection 

     $this->pdo = null; 

    } 

} 

回答

0

你叫$this->lasta->execute();,但你有沒有現場lasta

試試這個

$this->lastquery->execute(); 
+0

謝謝,但仍然不工作:「(我得到一個 我得到一個未捕獲的異常‘PDOException’有消息」 SQLSTATE [HY000]:常規錯誤:2053 –

+0

但它是另一個錯誤 – Stafox

+0

是的,當我做編輯到代碼 –

0

我會嘗試延長db_handle類和修改/創建一些方法,像這樣:

<?php 
// Make sure the db_handle is included and loaded before hand so it can be extended 
class QueryEngine extends db_handle 
    { 
     private $bind; 

     public function connect($host, $username, $password, $database) 
      { 
       // One note, I removed: 
       // $this->DBPrefix = $DBPrefix; 
       // $this->CHARSET = $CHARSET; 
       // You can add those back in if you want 

       try { 
        // Create connection 
        $opts = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
             PDO::ATTR_EMULATE_PREPARES => false, 
             PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC); 
        $this->pdo = new PDO('mysql:host='.$host.';dbname='.$database, $username, $password,$opts); 
       } 
       catch(PDOException $e) { 
        die($e->getMessage()); 
       } 
      } 

     public function query($query, $params = false) 
      { 
       if(!empty($params)) 
        $this->bindVals($params); 

       try { 
         if(!empty($this->bind)) { 
           $this->lastquery = $this->pdo->prepare($query); 
           $this->lastquery->execute($this->bind); 
          } 
         else 
          $this->lastquery = $this->pdo->query($query); 
        } 
       catch(PDOException $e) { 
        die($e->getMessage()); 
       } 

       return $this; 
      } 

     public function fetch() 
      { 
       while($row = $this->lastquery->fetch()) 
        $result[] = $row; 

       return (!empty($result))? $result : 0; 
      } 

     private function bindVals($params = false) 
      { 
       $this->bind = false; 

       if(empty($params) || !is_array($params)) 
        return $this; 

       $i = 0; 
       foreach($params as $values) { 
         $this->bind[':'.$i] = $values; 
         $i++; 
        } 

       return $this; 
      } 
    } 

要使用我們的新類:

$dbEngine = new QueryEngine(); 
$dbEngine->connect($host,$username,$password,$database); 
print_r($dbEngine->query("select * from users where ID = :0",array("1"))->fetch()); 

這將使你像(在我的分貝明顯,表和列會爲你不同)

Array 
(
    [0] => Array 
     (
      [ID] => 1 
      [unique_id] => 20150203190700523616 
      [username] => tester 
      [password] => $2a$12$xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
      [first_name] => Ras 
      [last_name] => Clatt 
      [email] => [email protected] 
      [usergroup] => 3 
      [user_status] => on 
      [reset_password] => $2y$10$xxxxxxxxxxxxxxxxxxx 
      [timestamp] => 2015-09-25 08:35:09 
     ) 

) 

該類你正在使用的庫與我的類似,所以我添加的是我使用的類的一部分。我測試了這個擴展類,它可以和我的數據庫一起工作,所以希望它能和你的數據庫一起工作!

相關問題