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内存表来代替php session的类

复制代码 代码如下:<?php /** @Usage: use some other storage method(mysql or memcache) instead of ph...

php调用MySQL存储过程的方法集合(推荐)

类型一:调用带输入、输出类型参数的方法复制代码 代码如下:$returnValue = '';try { mysql_query ( "set @Return" ); ...

PHP中mysqli_affected_rows作用行数返回值分析

本文实例分析了PHP中mysqli_affected_rows作用行数返回值。分享给大家供大家参考。具体分析如下: mysqli中关于update操作影响的行数可以有两种返回形式: 1....

php查询mysql大量数据造成内存不足的解决方法

本文实例分析了php查询mysql大量数据造成内存不足的解决方法。分享给大家供大家参考。具体分析如下: 一、问题 使用php查询mysql大数据量的时候,程序尚未执行完毕,跳出警告: F...

php数据入库前清理 注意php intval与mysql的int取值范围不同

php保存数据到mysql 打算在dao层进行数据入库前的清理,比如varchar进行trim,int进行intval。 有一天突然想起,php intval的取值范围与mysql的in...