php中mysql模組部分功能的簡單封裝

高洛峰
發布: 2023-03-01 13:10:02
原創
995 人瀏覽過

程式碼如下: 

class mysql 
{ 
private $db; // datebase connect 
private $result; // mysql result 
static private $mysql; // mysql object 
private function __construct() 
{ // The work before Create an object 
$this->db = mysql_connect('localhost','root',''); 
mysql_select_db('hello', $this->db ); 
} 
public static function getObject() 
{ //if have a object,return that object,Not create 
if(! self::$mysql instanceof self) 
self::$mysql = new self; 
return self::$mysql; 
} 
public function query($sql) 
{ 
$this->result = mysql_query($sql, $this->db); 
return $this->result; 
} 
public function fetch() 
{ 
if( isset($this->result ) ) 
return mysql_fetch_assoc( $this->result ); 
} 
public function error() 
{ 
return 'error:'.mysql_error(); 
} 
public function num() // for sql select result 
{ 
return mysql_num_rows( $this->result ); 
} 
public function close() 
{ // return true or false 
return mysql_close( $this->db ); 
} 
}
登入後複製


相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!