2011-11-01 97 views
1

当我与我的自定义功能,我得到这个错误执行的MySQLi查询...库MySQLi查询工作不正常

Fatal error: Call to a member function execute() on a non-object in /Applications/MAMP/htdocs/RevFramework/Application/Model/Engine/eMySQLi.php on line 92 

我真的不知道什么是错的......也就是说,如果我设置$类型和$ params为null,否则我相信call_user_func_array也会抛出一个错误。

public function query($SQL, $types = null, $params = null) 
   { 
           $this->result = $this->db->prepare($SQL); 
        
           if(isset($types) && isset($params)) 
           { 
               $bind_names[] = $types; 
                
               for ($i = 0; $i < count($params); $i++) 
               { 
                   $bind_name = 'bind' . $i; 
                   $$bind_name = $params[$i]; 
                   $bind_names[] = &$$bind_name; 
               } 
            
               call_user_func_array(array($this->result, 'bind_param'), $bind_names); 
           } 

           $this->result->execute(); 
   } 

查询是这样做的:

$class->query("SELECT name FROM rev_widgets ORDER BY order"); 

我完全不知道什么可能是错误的。

任何帮助表示赞赏!

+2

行$ this-> result = $ this-> db-> prepare($ SQL);'没有返回你所期望的。你可以发布'vardump($ this-> result)'的输出吗?此外,您可能希望查阅[PDO :: prepare手册](http://php.net/manual/en/pdo.prepare.php)以获取关于它抛出的错误和异常的更多信息。 –

+0

'var_dump($ this-> result);'returns'bool(false)'。而且,谢谢,但我使用MySQLi,将检查它。 –

+1

bool(false)表示您的'result'字段不包含任何对象。也许你的'db-> prepare()'方法失败了。 – ariefbayu

回答

0

我找到了答案!那么,我只是继续前进,做得非常不同。

对于每个需要它的人,现在看起来都是这样。

class Core_Model extends Core_Controller 
{ 

     private $connected; 

     private $db; 

     protected $result; 

    public function __construct() 
    { 
      //$this->connect(); 
    } 

     protected function connect() 
     { 
      if($this->connected != true) 
      { 
       $this->db = new mysqli($this->data['host'], $this->data['user'], $this->data['pass'], $this->data['database']); 

       if($this->mysqli->connect_errno)  
       { 
        $this->error($mysqli->connect_errno); 
       } 

       $this->connected = true; 
      } 
     } 

     protected function disconnect() 
     { 
      if($this->connected == true) 
      { 
       $this->db->close(); 
       $this->connected = false; 
      } 
     } 

     public function newQuery() 
     { 
      $this->connect(); 

      if(is_object($this->result)) 
      { 
       $this->result->close(); 
      } 

      return $this; 
     } 

     public function query($SQL, $params = null) 
     { 
      $this->newQuery(); 

      if(($this->result = $this->db->prepare($SQL))) 
      { 
       if($params != null) 
       { 
        call_user_func_array(array($this->result, 'bind_param'), $this->refreshParams($params)); 
       } 

       $this->result->execute(); 
      } 
      else 
      { 
       trigger_error("MySQLi query <i> '" . $SQL . "'</i> failed", E_USER_ERROR); 
      } 

      return $this; 
     } 

     public function get() 
     { 
      $parameters = $this->getFieldNames(); 
      call_user_func_array(array($this->result, 'bind_result'), $this->refreshParams($parameters)); 

      while($this->result->fetch()) 
      { 
       $x = array(); 

       foreach($this->row as $key => $val) 
       { 
        $x[$key] = $val; 
       } 

       $data[] = $x; 
      } 

      return $data; 
     } 

     public function id() 
     { 
      return $this->mysql->insert_id; 
     } 

     public function num_rows() 
     { 
      $this->result->store_result(); 

      return $this->result->num_rows; 
     } 


    private function getFieldNames() 
    { 
      $meta = $this->result->result_metadata(); 

      while($field = $meta->fetch_field()) 
      { 

       $parameters[] = &$this->row[$field->name]; 

      } 

      return $parameters; 
     } 

     private function refreshParams($params) 
     { 
      $temp = array(); 

      foreach($params as $key => $value) 
      { 
       $temp[$key] = &$params[$key]; 
      } 

      return $temp; 
     } 
} 

希望这可以帮助别人!