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数据到Excel文件(fputcsv)

这里的方法是利用fputcsv写CSV文件的方法,直接向浏览器输出Excel文件。 复制代码 代码如下: // 输出Excel文件头,可把user.csv换成你要的文件名 header(...

深入理解php的MySQL连接类

无意间在电脑里发现还有这么个Mysql的连接类,也不记得哪里收藏的了,贴上来吧。后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜...

php环境配置 php5 mysql5 apache2 phpmyadmin安装与配置

php环境配置 php5 mysql5 apache2 phpmyadmin安装与配置

php环境的配置,对于新手来说,绝对是一件烦事。总会遇到这样那样的问题,走很多弯路。所以今天特意写了这个配置文档,相信按照以下步骤你一定会成功的。错误的地方也希望各位指正。 更多文章 p...

Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)

Mysql的Root密码忘记,查看或修改的解决方法(图文介绍)

首先启动命令行 1.在命令行运行:taskkill /f /im mysqld-nt.exe 下面的操作是操作mysql中bin目录下的一些程序,如果没有配置环境变量的话,需要切换到...

PHP将MySQL的查询结果转换为数组并用where拼接的示例

mysql查询结果转换为PHP数组的几种方法的区别:  $result = mysql_fetch_row():这个函数返回的是数组,数组是以数字作为下标的,你只能通过...