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中BLOB字段的方法示例【存储文本与图片】

本文实例讲述了PHP操作MySQL中BLOB字段的方法。分享给大家供大家参考,具体如下: 1、MySQL中BLOB字段类型 BLOB类型的字段用于存储二进制数据。 MySQL中,BLOB...

PHP读取ACCESS数据到MYSQL的代码

复制代码 代码如下: <?php header('ontent-Type:text/html;charset=GB2312');//避免输出乱码 $dbhost ="localho...

PHP把MSSQL数据导入到MYSQL的方法

本文实例讲述了PHP把MSSQL数据导入到MYSQL的方法。分享给大家供大家参考。具体分析如下: 最近需要把一个以前的asp网站转换成php的,但php是与mysql而我的asp与mss...

php中转义mysql语句的实现代码

你总不可能对每一个这样的特殊字符都人工进行转义,何况你通常处理的都是表单自动提交的内容。 所以,应该使用mysql_real_escape_string函数: mysql_real_es...

PHP基于单例模式实现的mysql类

本文实例讲述了PHP基于单例模式实现的mysql类。分享给大家供大家参考,具体如下: <?php defined('ACC')||exit('Access Denied...