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 Date/Time 类型

 由于曾经和他是同一个团队的,所以对于其我很熟悉他那“洁癖”的做法,对于他的很多的观点我也非常的赞同;但是有一件非常不理解的地方就是设计数据库的时候总是会回避使用 D...

MySQL 日期时间函数常用总结

MySQL 日期时间函数常用总结

 获得当前日期+时间(date + time)1.1    函数:now()  相关函数:current...

PHP操作MySQL事务实例

本文实例讲述了PHP操作MySQL事务的方法,分享给大家供大家参考。具体方法如下: 一般来说,事务都应该具备ACID特征。所谓ACID是Atomic(原子性),Consistent(一致...

php+Mysqli利用事务处理转账问题实例

本文实例讲述了php+Mysqli利用事务处理转账问题的方法。分享给大家供大家参考。具体实现方法如下: <?php header("Content-type:te...

自写的利用PDO对mysql数据库增删改查操作类

前言 PDO一是PHP数据对象(PHP Data Object)的缩写。 并不能使用PDO扩展本身执行任何数据库操作,必须使用一个database-specific PDO driver...