PHP MySQL模块部分功能的简单封装
class mysql
{
private $db; // 数据库连接
private $result; // MySQL结果集
static private $mysql; // MySQL对象
private function __construct()
{ // 创建对象前的准备工作
$this->db = mysql_connect('localhost','root','');
mysql_select_db('hello', $this->db );
}
public static function getObject()
{ // 如果已有对象,则返回该对象,不重新创建
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() // 用于SQL SELECT结果
{
return mysql_num_rows( $this->result );
}
public function close()
{ // 返回true或false
return mysql_close( $this->db );
}
}
这样做看起来就只对可移植有用,其它的作用还体会不到