tags:

views:

23

answers:

2

As far as I've done my searchings over Google and everything, it looks like this is a pretty common issue, but I cannot seem to fix it. Plus, I think that I have a little different usage of the function as others out there. And, after about 3 hours of no luck, I'm posting here!

function free_result(){                                        # LINE 48
    $free = "SELECT SHOW DESCRIBE EXPLAIN";                    # LINE 49
    $free = explode(" ", $free);                               # LINE 50
    $sql = $this->sql;                                         # LINE 51
    while(list($key, $value) = each($free)){                   # LINE 52
        if(preg_match("/\b".$value."\b/", $sql)){              # LINE 53
            $result = $this->result;                           # LINE 54
            if(!mysqli_free_result($result)){                  # LINE 55
                $this->errors("Invalid result: <b>{$result}</b>. Couldn't free result."); # LINE 56
            }                                                  # LINE 57
        }                                                      # LINE 58
    }                                                          # LINE 59
}                                                              # LINE 60
                                                               # LINE 61
function query($sql){                                          # LINE 62
    $this->query_id = mysqli_query($this->connection, $sql);   # LINE 63
    $this->result = mysqli_store_result($this->connection);    # LINE 64
    $this->sql = $sql;                                         # LINE 65
    if(!$this->query_id){                                      # LINE 66
        $this->errors("Couldn't query: <b>{$sql}</b>");        # LINE 67
        return 0;                                              # LINE 68
    }                                                          # LINE 69
    $this->affected = mysqli_affected_rows($this->connection); # LINE 70
                                                               # LINE 71
    return $this->query_id;                                    # LINE 72
}                                                              # LINE 73

These are 2 of the functions which are within my database class. But I think only these 2 are needed for resolving the issue.

So, the error I'm recieving is:

"Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, 
boolean given in [file path]\database.class.php  on line 55"
#followed by my database class error handling
"Invalid result: . Couldn't free result."

As far as my understanding about this goes, I think that the problem is with $result variable (LINE 54, LINE 64), but because this is my first adventure with MySQLi then I'm not quite sure.

I hope you understand the issue and will be able to help! Thanks in advance!

A: 

That code looks correct, except that *store_result* can return FALSE when there's no result set (e.g. for an insert) or an error. If mysqli_field_count is non-zero, it should have returned a result set. It looks like you're checking for this using your $free array (but field_count should be faster and more accurate). If mysqli_error returns a non-empty string, there was an error.

Matthew Flaschen
+1  A: 

mysqli_store_result returns a boolean in 2 cases: for update / insert / delete etc. queries it returns true (and those queries could contain a SELECT/SHOW/DESCRIBE/EXPLAIN in a string for instance), or a SELECT/SHOW/DESCRIBE/EXPLAIN query fails. Inspecting $this->sql will show you which. If you've got your heart set in freeing those results, just do a more simple check on it before:

 function free_result(){ 
    if($this->result instanceof mysqli_result) $this->result->free();
 }
Wrikken
Thanks! Works perfectly... Again it's proven that simple is better. Hehe.
Tom