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

yipeiwu_com5年前Mysql基础
复制代码 代码如下:

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无法访问远程mysql的问题分析及解决

首先说明,远程服务器是可远程访问的。 我遇到的问题是这样的:有A,B,C三台服务器,C为服务器,B可以用PHP成功连接上C机器的mysql,而A机器则不能连接! 可以说,肯定不是代码有什...

PHP中addslashes与mysql_escape_string的区别分析

本文实例分析了PHP中addslashes与mysql_escape_string的区别。分享给大家供大家参考,具体如下: 1.在插入数据时两者的意义基本一样.区别只在于addslash...

php数据入库前清理 注意php intval与mysql的int取值范围不同

php保存数据到mysql 打算在dao层进行数据入库前的清理,比如varchar进行trim,int进行intval。 有一天突然想起,php intval的取值范围与mysql的in...

PHP+MYSQL 出现乱码的解决方法

肯定都已经解决了JSP里的乱码问题,不过还是有些人的MYSQL乱码问题没有得到解决,包括我自己,所以又找了一些资料,希望这次能完全解决MYSQL数据库的乱码问题。  第一种方法...

php Mysql日期和时间函数集合

收集的比较全的Mysql日期和mysql时间函数 DATE_FORMAT(date,format)  根据format字符串格式化date值。下列修饰符可以被用在form...