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 $con = mysql_connect("localhost:3306","root",""); if (!$con) { d...

PHP程序员最常犯的11个MySQL错误小结

对于很多新手们来说,使用PHP可以在短短几个小时之内轻松地写出具有特定功能的代码。但是,构建一个稳定可靠的数据库却需要花上一些时日和相关技能。下面列举了我曾经犯过的最严重的11个MySQ...

php和mysql中uft-8中文编码乱码的几种解决办法

PHP页面转UTF-8编码问题 1.在代码开始出加入一行: 复制代码 代码如下: header("Content-Type: text/html;charset=utf-8"); 2....

PHP使用Mysqli类库实现完美分页效果的方法

PHP使用Mysqli类库实现完美分页效果的方法

本文实例讲述了PHP使用Mysqli类库实现完美分页效果的方法。分享给大家供大家参考,具体如下: 本篇文章是基于的是我的上篇文章《PHP数据库操作之基于Mysqli的数据库操作类库》而量...

Excel数据导入Mysql数据库的实现代码

    首先做一下说明,为什么我要用Navicat,第一个原因,因为它是个不错的Mysql GUI工具,更重要的是,它可以将一些外部数据...