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使用redis实现统计缓存mysql压力的方法

本文实例讲述了PHP使用redis实现统计缓存mysql压力的方法。分享给大家供大家参考,具体如下: <?php header("Content-Type:text...

php简单备份与还原MySql的方法

本文实例讲述了php简单备份与还原MySql的方法。分享给大家供大家参考,具体如下: 一、备份: <?php header ( "content-Type: text/...

PHP 使用MySQL管理Session的回调函数详解

复制代码 代码如下:<?php class MySession extends DBSQL {  /**   * __constr...

php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例

php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率完整示例

本文实例讲述了php使用mysqli和pdo扩展,测试对比mysql数据库的执行效率。分享给大家供大家参考,具体如下: <?php /** * 测试pdo和mysql...

PHP+mysql防止SQL注入的方法小结

本文实例讲述了PHP+mysql防止SQL注入的方法。分享给大家供大家参考,具体如下: SQL注入 例:脚本逻辑 $sql = "SELECT * FROM user WHERE u...