2010-07-16 29 views
1

就我對Google和所有內容所做的搜索而言,它看起來像是一個很常見的問題,但似乎無法修復它。另外,我認爲我和其他人有不同的用法。 而且,經過大約3個小時的運氣,我在這裏張貼!mysqli_free_result():mysqli_result類的對象無法轉換爲字符串

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 

這些是我的數據庫類中的2個函數。但我認爲只有這2個才能解決問題。

所以,我recieving錯誤是:

"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." 

至於我對這個去理解,我覺得現在的問題是$結果變量(54行,64行),但因爲這是我對MySQLi的第一次冒險,所以我不太確定。

我希望你能理解這個問題,並且能夠提供幫助! 在此先感謝!

回答

1

mysqli_store_result返回2例一個布爾值:用於更新/插入/刪除等查詢它返回true(和這些查詢可以包含一個SELECT/SHOW/DESCRIBE /解釋,例如一個字符串),或SELECT/SHOW/DESCRIBE/EXPLAIN查詢失敗。檢查$this->sql會告訴你哪個。如果你已經開始解放這些結果了,只需在之前做一個更簡單的檢查:

function free_result(){ 
    if($this->result instanceof mysqli_result) $this->result->free(); 
} 
+0

謝謝!完美的作品...再次證明簡單更好。呵呵。 – jolt 2010-07-16 02:21:20

0

該代碼看起來是正確的,但store_result可以在沒有結果集(例如插入)或錯誤時返回FALSE。如果mysqli_field_count非零,則應該已返回結果集。它看起來像使用$ free數組檢查這個(但field_count應該更快,更準確)。如果mysqli_error返回一個非空字符串,則出現錯誤。

相關問題