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

yipeiwu_com5年前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中文乱码的问题。   以前的国外主机用的Mysql是4.x系列的,感觉还比较好,都无论GBK和UTF-8都没有乱码,没想到新的主机的M...

PHP入门教程之使用Mysqli操作数据库的方法(连接,查询,事务回滚等)

本文实例讲述了PHP入门教程之使用Mysqli操作数据库的方法。分享给大家供大家参考,具体如下: Demo1.php <?php //使用 mysqli 对象操作数...

常用的PHP数据库操作方法(MYSQL版)

一、数据库操作 1. 连接MYSQL数据 mysql_connect() e.g. 复制代码 代码如下: $db = mysql_connect(MYSQL_HOST, MYSQL_US...

PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】

本文实例讲述了PHP基于pdo的数据库操作类。分享给大家供大家参考,具体如下: 工作中需要操作sqlserver、oracle都是使用的这个类,当时是在别人的基础上改进了,现在分享下...

php使用mysqli向数据库添加数据的方法

本文实例讲述了php使用mysqli向数据库添加数据的方法。分享给大家供大家参考。具体实现方法如下: $mydb = new mysqli('localhost', 'usernam...