php中mysql模块部分功能的简单封装

高洛峰
高洛峰 原创
2023-03-01 13:10:02 776浏览

代码如下:

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 ); 
} 
}


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。