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分页代码详解

复制代码 代码如下:<?php    $perpagenum = 10;//定义每页显示几条   ...

PHP格式化MYSQL返回float类型的方法

本文实例讲述了PHP格式化MYSQL返回float类型的方法。分享给大家供大家参考,具体如下: PHP 中获取mysql的float字段,echo 输出后,小数部分为包含多个0. 可使用...

PHP中使用sleep造成mysql读取失败的案例和解决方法

近日,由于项目需求 需要用到sleep函数定时从数据库取一堆数据出来去执行某些操作。 sleep等待的时间至少有一个小时以上 此前做过测试 用sleep函数去完成数小时后执行的操作是可行...

php写入mysql中文乱码的实例解决方法

php写入mysql出现中文乱码的解决办法是:在建立数据库连接之后,将该连接的编码方式改为中文。 代码如下: $linkID=@mysql_connect("localhost","...

php+mysql结合Ajax实现点赞功能完整实例

本文实例讲述了php+mysql结合Ajax实现点赞功能的方法。分享给大家供大家参考。具体如下: 要实现点赞功能,有多种实现方式,这里总结一下利用Ajax,php和mysql来实现点赞的...