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

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

相关文章

PHP递归写入MySQL实现无限级分类数据操作示例

本文实例讲述了PHP递归写入MySQL实现无限级分类数据操作。分享给大家供大家参考,具体如下: PHP递归写入MySQL无限级分类数据,表结构: CREATE TABLE `kepl...

PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】

本文实例讲述了PHP操作MySQL中BLOB字段的方法。分享给大家供大家参考,具体如下: 1、MySQL中BLOB字段类型 BLOB类型的字段用于存储二进制数据。 MySQL中,BLOB...

非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程

非集成环境的php运行环境(Apache配置、Mysql)搭建安装图文教程

本文为大家分享了php运行环境搭建安装图文教程,供大家参考,具体内容如下 安装apache: 1,不要安装到有中文的目录中: 2,尽量将apache,php,mysql安装到一个总的目录...

解析csv数据导入mysql的方法

mysql自己有个csv引擎,可以通过这个引擎来实现将csv中的数据导入到mysql数据库中,并且速度比通过php或是python写的批处理程序快的多。具体的实现代码示例:复制代码 代码...

PHP商品秒杀问题解决方案实例详解【mysql与redis】

本文实例讲述了PHP商品秒杀问题解决方案。分享给大家供大家参考,具体如下: 引言 假设num是存储在数据库中的字段,保存了被秒杀产品的剩余数量。 if($num > 0){...