Call to undefined method mysqli_stmt::get_result

后端 未结 10 951
难免孤独
难免孤独 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:22

    for those searching for an alternative to $result = $stmt->get_result() I've made this function which allows you to mimic the $result->fetch_assoc() but using directly the stmt object:

    function fetchAssocStatement($stmt)
    {
        if($stmt->num_rows>0)
        {
            $result = array();
            $md = $stmt->result_metadata();
            $params = array();
            while($field = $md->fetch_field()) {
                $params[] = &$result[$field->name];
            }
            call_user_func_array(array($stmt, 'bind_result'), $params);
            if($stmt->fetch())
                return $result;
        }
    
        return null;
    }
    

    as you can see it creates an array and fetches it with the row data, since it uses $stmt->fetch() internally, you can call it just as you would call mysqli_result::fetch_assoc (just be sure that the $stmt object is open and result is stored):

    //mysqliConnection is your mysqli connection object
    if($stmt = $mysqli_connection->prepare($query))
    {
        $stmt->execute();
        $stmt->store_result();
    
        while($assoc_array = fetchAssocStatement($stmt))
        {
            //do your magic
        }
    
        $stmt->close();
    }
    

提交回复
热议问题