douqingnao9246 2011-11-01 20:49
浏览 68
已采纳

MySQLi查询无法正常工作

When I execute a MySQLi query with my custom function I get this error...

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

I really don't know what's wrong... That is if I set $types and $params to null, else I'm sure call_user_func_array would also throw an error.

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

The query it is doing:

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

I have absolutely no idea what could be wrong.

Any help is appreciated!

  • 写回答

1条回答 默认 最新

  • doumi1852 2011-11-14 05:38
    关注

    I found the answer! Well, I just went ahead and did it very differently.

    For everyone who needs it, it all looks like this now.

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

    Hope this helps someone!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。