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数据库的php代码(一个表一个文件)

复制代码 代码如下: <?php $cfg_dbhost ='localhost';//mysql主机 $cfg_dbname ='sq_test';//数据库名 $cfg_dbu...

攻克CakePHP系列一 连接MySQL数据库

攻克CakePHP系列一 连接MySQL数据库

先阅读这个https://www.jb51.net/article/16244.htm接下来将一步一步对这个框架进行说明。 首先是下载框架:官方主页:http://cakephp.org...

PHP使用redis实现统计缓存mysql压力的方法

本文实例讲述了PHP使用redis实现统计缓存mysql压力的方法。分享给大家供大家参考,具体如下: <?php header("Content-Type:text...

10个可以简化php开发过程的MySQL工具

MySQL Workbench   MySQL Workbench是一个由MySQL开发的跨平台、可视化数据库工具。它作为DBDesigner4工程的替代应用程序而备受瞩目。M...

mac下Apache + MySql + PHP搭建网站开发环境

首先为什不自己分别搭建Apache,PHP和MySql的环境呢?这样自己可以了解更多知识,说起来也更酷。可也许因为我懒吧,我是那种“既然有现成的,用就是了”的人。君子生非异也,善假于物也...