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 );
}
}

这样做看起来就只对可移植有用,其它的作用还体会不到

相关文章

mysql_connect localhost和127.0.0.1的区别(网络层阐述)

connects.php 复制代码 代码如下: mysql_connect('127.0.0.1','root','zzzizzz1'); mysql_connect('localhos...

Windows下PHP开发环境搭建教程(Apache+PHP+MySQL)

Windows下PHP开发环境搭建教程(Apache+PHP+MySQL)

由于换电脑或重装系统后常需要重新搭建PHP环境,此次简单记录一下Windows下搭建PHP环境的过程,具体步骤可以参照网上资料 准备工作: Windows下手工搭建PHP环境需要先下载相...

mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别

复制代码 代码如下:<?php $link=mysql_connect('localhost','root',”); mysql_select_db('abc',$link); $...

php+mysqli使用面向对象方式更新数据库实例

本文实例讲述了php+mysqli使用面向对象方式更新数据库的方法,分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<?php //第一步:创建数据连接对象...

php封装的mysqli类完整实例

本文实例讲述了php封装的mysqli类。分享给大家供大家参考,具体如下: 类: <?php header('content-type:text/html;charse...