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

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

相关文章

PHP7.0连接DB操作实例分析【基于mysqli】

PHP7.0连接DB操作实例分析【基于mysqli】

本文实例讲述了PHP7.0连接DB操作。分享给大家供大家参考,具体如下: PHP <?php //连接数据库 $link = mysqli_connect('local...

实现 win2003 下 mysql 数据库每天自动备份

1. 环境: windows server 2003 +  PHP5 + MySQL ...

php实现mysql同步的实现方法

拿到需求之后,发现这两个网站的MYSQL数据库都不能远程访问(安全第一吧)。于是想起了 平时使用的CSV文件批量录入数据。于是 尝试使用CSV导入导出。 导入到处框架如下: 1首先将数据...

PHP+MySQL实现消息队列的方法分析

本文实例讲述了PHP+MySQL实现消息队列的方法。分享给大家供大家参考,具体如下: 最近遇到一个批量发送短信的需求,短信接口是第三方提供的。刚开始想到,获取到手机号之后,循环调用接口发...

如何设置mysql允许外网访问

mysql的root账户,我在连接时通常用的是localhost或127.0.0.1,公司的测试服务器上的mysql也是localhost所以我想访问无法访问,测试暂停. 解决方法: 1...