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中UTF8编码的中文排序实例

本文实例讲述了PHP与MYSQL中UTF8编码的中文排序方法,分享给大家供大家参考。具体实现方法如下: 一般来说,中文共有三种排序方式: 1.根据拼音排序; 2.根据笔画排序; 3.根据...

mysql_num_rows VS COUNT 效率问题分析

mysql_num_rows 和 count( * ) 都能统计总数,那个能好一点呢? 或者 分别什么时候用num_rows 和 count( * )呢 一个直观的对比 测试数据: 条数...

php列出mysql表所有行和列的方法

本文实例讲述了php列出mysql表所有行和列的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<html> <head> <title...

mysql中存储过程、函数的一些问题

最近写一些mysql的存储过程和函数,发现网上比较有价值的文档很少,大都是照着手册上抄来的,有些实际问题解决不了,比如用变量作表名。 经过反复调试,总算找到解决办法,一下是一些简单的记录...

PHP+MySQL实现的简单投票系统实例

本文实例讲述了PHP+MySQL实现的简单投票系统。分享给大家供大家参考,具体如下: <html> <head><title>手机系统调查问卷&l...