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

yipeiwu_com6年前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 );
}
}

这样做看起来就只对可移植有用,其它的作用还体会不到

相关文章

MySql 按时间段查询数据方法(实例说明)

时间格式为2008-06-16 查询出当天数据: SELECT * FROM `table` WHERE date(时间字段) = curdate(); 查询出当月字段: SELECT...

Mac环境下php操作mysql数据库的方法分享

Mac环境下php操作mysql数据库的方法分享

Mac本地环境搭建 在Mac系统,我们可以使用MAMP Pro 软件来搭建本地服务器。安装好这款软件,网站的目录在 /Applications/MAMP/htdocs 文件夹里,只需将文...

php从memcache读取数据再批量写入mysql的方法

本文实例讲述了php从memcache读取数据再批量写入mysql的方法。分享给大家供大家参考。具体分析如下: 用 Memcache 可以缓解 php和数据库压力下面代码是解决高负载下数...

PHP和Mysql中转UTF8编码问题汇总

一个网站如果需要国际化,就需要将编码从GB2312转成UTF-8,其中有很多的问题需要注意,如果没有转换彻底,将会有很多的编码问题出现! PHP页面转UTF-8编码问题 1.在代码开始出...

php中选择什么接口(mysql、mysqli)访问mysql

我们知道,mysqli是PHP 5中新提供的MySQL接口,此接口使用了面向对象的思想。使用mysqli接口的代码可读性更强,其执行效率比mysql接口高。而且mysqli提供了一个能够...