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,access,sqlserver,Oracle)

1、PHP连接MYSQL数据库的代码 <?php $mysql_server_name='localhost'; //改成自己的mysql数据库服务器 $mys...

解析csv数据导入mysql的方法

mysql自己有个csv引擎,可以通过这个引擎来实现将csv中的数据导入到mysql数据库中,并且速度比通过php或是python写的批处理程序快的多。具体的实现代码示例:复制代码 代码...

php 随机记录mysql rand()造成CPU 100%的解决办法

百度查阅了一些资料,再结合自己的一些经验,采用以下解决办法: 复制代码 代码如下: $idlist=''; for($i=1;$i<=20;$i++){ if($i==1){ $i...

php MySQL与分页效率

 最基本的分页方式: SELECT ... FROM ... WHERE ... ORDER BY&nbs...

Win2003下IIS+PHP+MySQL+Zend配置步骤详解第1/2页

一、软件的获取 1.php首先去http://www.php.net/downloads.php下载最新的PHP 5.2.0版本。 2.MySQL可以在http://dev.m...