Call to undefined method mysqli_stmt::get_result

后端 未结 10 924
难免孤独
难免孤独 2020-11-21 05:10

Here\'s my code:

include \'conn.php\';
$conn = new Connection();
$query = \'SELECT EmailVerified, Blocked FROM users WHERE Email = ? AND SLA = ? AND `Passwo         


        
10条回答
  •  隐瞒了意图╮
    2020-11-21 05:29

    Here is my alternative. It is object-oriented and is more like mysql/mysqli things.

    class MMySqliStmt{
        private $stmt;
        private $row;
    
        public function __construct($stmt){
            $this->stmt = $stmt;
            $md = $stmt->result_metadata();
            $params = array();
            while($field = $md->fetch_field()) {
                $params[] = &$this->row[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $params) or die('Sql Error');
        }
    
        public function fetch_array(){
            if($this->stmt->fetch()){
                $result = array();
                foreach($this->row as $k => $v){
                    $result[$k] = $v;
                }
                return $result;
            }else{
                return false;
            }
        }
    
        public function free(){
            $this->stmt->close();
        }
    }
    

    Usage:

    $stmt = $conn->prepare($str);
    //...bind_param... and so on
    if(!$stmt->execute())die('Mysql Query(Execute) Error : '.$str);
    $result = new MMySqliStmt($stmt);
    while($row = $result->fetch_array()){
        array_push($arr, $row);
        //for example, use $row['id']
    }
    $result->free();
    //for example, use the $arr
    

提交回复
热议问题