MySQLi-запрос не работает должным образом - PullRequest
1 голос
/ 02 ноября 2011

Когда я выполняю 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

Я действительно не знаю, что не так ... То есть, если я устанавливаю $ types и $ paramsк нулю, иначе я уверен, что 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");

Я понятия не имею, что может быть не так.

Любая помощь приветствуется!

1 Ответ

0 голосов
/ 14 ноября 2011

Я нашел ответ!Ну, я просто пошел вперед и сделал это совсем по-другому.

Для всех, кому это нужно, все теперь выглядит так.

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

Надеюсь, это кому-нибудь поможет!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...