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使用mysqli操作MySQL数据库的简单方法

PHP的 mysqli 扩展提供了其先行版本的所有功能,此外,由于 MySQL 已经是一个具有完整特性的数据库服务器 , 这为PHP 又添加了一些新特性 。 而 mysqli 恰恰也支持...

php实现的mysqldb读写分离操作类示例

本文实例讲述了php实现的mysqldb读写分离操作类。分享给大家供大家参考,具体如下: /** * php MysqlDB 读写分离类 * --------------------...

PHP+mysql实现从数据库获取下拉树功能示例

本文实例讲述了PHP+mysql实现从数据库获取下拉树功能。分享给大家供大家参考,具体如下: <?php include "config.php"; include "...

MYSQL 小技巧 -- LAST_INSERT_ID

其实,这两个是有区别的,LAST_INSERT_ID() 能返回 bigint 值的id。而,mysql_insert_id 返回的是 int 。如果你 的id 是 unsigned i...

PHP调用MySQL的存储过程的实现代码

MySQL好像从5.0开始才引入存储过程,反正以前做应用的时候从没碰过,不过现在因为主要作内部系统,所以很多应用都用到了存储过程,当然前台有时候也需要调用MySQL存储过程,PHP的My...