關於釋放資料庫結果集的有關問題

來源:互聯網
上載者:User
關於釋放資料庫結果集的問題。
class DB{
private $host;//服務區地址
private $user;//登入帳號
private $pass;//登入密碼
private $name;//資料庫名稱
private $font;//資料庫字元集
private $conn;//資料庫連接源
private $result;//結果集
private $rowRst;//查詢單條記錄
private $rowArray=array();//查詢多條記錄
private $rowAff;//增改刪的記錄條數

public function __construct($host,$user,$pass,$name,$font){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->name=$name;
$this->font=$font;
$this->init_conn();
}

//串連資料庫
private function init_conn(){
if(!$this->conn=mysql_connect($this->host,$this->user,$this->pass)){
exit('資料庫連接錯誤');
}
if(!mysql_select_db($this->name,$this->conn)){
exit('資料庫選擇錯誤');
}
if(!mysql_query('SET NAMES '.$this->font)){
exit('字元集錯誤');
}
}

//執行sql語句
public function _query($sql){
if(!$this->result=mysql_query($sql,$this->conn)){
exit('sql執行失敗');
}
return $this->result;
}

//查詢單挑資料
public function _fetch_array($sql){
$this->_query($sql);
$this->rowRst=mysql_fetch_array($this->result,MYSQL_ASSOC);
return $this->rowRst;
}

//查詢多條資料
public function _fetch_array_list($sql){
$this->_query($sql);
while (!!$rows=mysql_fetch_array($this->result,MYSQL_ASSOC)){
$this->rowArray[]=$rows;
}
return $this->rowArray;
}

//增改刪的影響記錄條數
public function _affected_rows(){
$this->rowAff=mysql_affected_rows();
return $this->rowAff;
}

//取得上一步INSERT操作的ID號
public function _insert_id(){
return mysql_insert_id();
}

//釋放結果集
public function _free_result(){
$this->rowAff='';
$this->rowArray='';
$this->rowRst='';
mysql_free_result($this->result);
}

//關閉資料庫
public function _close_conn(){
$this->_free_result();
mysql_close($this->conn);
}
}
當我用_close_conn這個方法是,網頁面顯示mysql_free_result():supplied argument is not a valid mysql result resource
如何解決這個問題


------解決方案--------------------
在mysql_free_result($this->result); 之前你先看一下 $this->result 變成什麼了。是不是你原先開啟的資源
------解決方案--------------------
if(is_resource($this->result))
{
mysql_free_result($this->result);
$this->result = NULL;
}
------解決方案--------------------
你的 $this->result 是這樣賦值的
$this->result=mysql_query($sql,$this->conn)

mysql_query 只在執行 select 語句時返回的是資源,其他的都是邏輯值

所以 mysql_free_result($this->result);
要寫作
if(is_resource($this->result)) mysql_free_result($this->result);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.