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数据的操作要点

MySQL扩展库操作MySQL数据库的步骤如下: 1:获取连接. 2:选取书库。 3:设置操作编码。 4:发送SQL指令(MySQL数据库可以分为四种指令: 4.1:ddl: 数据定义语...

PHP手机号码归属地查询代码(API接口/mysql)

首先我们介绍使用自己的数据库查询多个手机号码,那还是建议你拥有一个自己的的手机号码数据库。正常情况下,只是满足一般查询的话,你不需要去购买专业版的手机号码数据库,增加无谓成本。我免费为你...

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

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

PHP同时连接多个mysql数据库示例代码

实例: 复制代码 代码如下: <?php $conn1 = mysql_connect("127.0.0.1", "root","root","db1"); mysql_selec...

PHP中使用localhost连接Mysql不成功的解决方法

发现问题 昨天在帮同事编译安装Linux环境时,遇到一个问题: WEB服务器是apache,数据库是MySQL。 于是写了一个测试连接数据库的PHP页面: 复制代码 代码如下:$mysq...