mysql类

class mysql extends db {
    private static $ins = NULL;
    private $conn = NULL;
    private $conf = array();

    protected function __construct() {
        $this->conf = conf::getIns();

        $this->connect($this->conf->host,$this->conf->user,$this->conf->pwd);
        $this->select_db($this->conf->db);
        $this->setChar($this->conf->char);
    }

    public static function getIns() {
        if(!(self::$ins instanceof self)) {
            self::$ins = new self();
        }

        return self::$ins;
    }

    public function connect($h,$u,$p) {
        $this->conn = mysql_connect($h,$u,$p);
        if(!$this->conn) {
            $err = new Exception(‘连接失败‘);
            throw $err;
        }
    }

    protected function select_db($db) {
        $sql = ‘use ‘ . $db;
        $this->query($sql);
    }

    protected function setChar($char) {
        $sql = ‘set names ‘ . $char;
        return $this->query($sql);
    }

    public function query($sql) {

        $rs = mysql_query($sql,$this->conn);

        log::write($sql);

        return $rs;
    }

    public function autoExecute($table,$arr,$mode=‘insert‘,$where = ‘ where 1 limit 1‘) {
        /*    insert into tbname (username,passwd,email) values (‘‘,)
        /// 把所有的键名用‘,‘接起来
        // implode(‘,‘,array_keys($arr));
        // implode("‘,‘",array_values($arr));
        */

        if(!is_array($arr)) {
            return false;
        }

        if($mode == ‘update‘) {
            $sql = ‘update ‘ . $table .‘ set ‘;
            foreach($arr as $k=>$v) {
                $sql .= $k . "=‘" . $v ."‘,";
            }
            $sql = rtrim($sql,‘,‘);
            $sql .= $where;

            return $this->query($sql);
        }

        $sql = ‘insert into ‘ . $table . ‘ (‘ . implode(‘,‘,array_keys($arr)) . ‘)‘;
        $sql .= ‘ values (\‘‘;
        $sql .= implode("‘,‘",array_values($arr));
        $sql .= ‘\‘)‘;

        return $this->query($sql);

    }

    public function getAll($sql) {
        $rs = $this->query($sql);

        $list = array();
        while($row = mysql_fetch_assoc($rs)) {
            $list[] = $row;
        }

        return $list;
    }

    public function getRow($sql) {
        $rs = $this->query($sql);

        return mysql_fetch_assoc($rs);
    }

    public function getOne($sql) {
        $rs = $this->query($sql);
        $row = mysql_fetch_row($rs);

        return $row[0];
    }

    // 返回影响行数的函数
    public function affected_rows() {
        return mysql_affected_rows($this->conn);
    }

    // 返回最新的auto_increment列的自增长的值
    public function insert_id() {
        return mysql_insert_id($this->conn);
    }

}

  

时间: 2025-01-17 17:35:22

mysql类的相关文章

C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </sum

C#---数据库访问通用类、Access数据库操作类、mysql类 .

//C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </summary&

面向对象联系(定义mysql类)

练习1:定义MySQL类 要求: 1.对象有id.host.port三个属性 2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一 3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化 4.为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常,;get_obj_by_id方法用

简单的一个MySQL类的实现:

'''定义MySQL类:1.对象有id.host.port三个属性2.定义工具create_id,在实例化时为每个对象随机生成id,保证id唯一3.提供两种实例化方式,方式一:用户传入host和port 方式二:从配置文件中读取host和port进行实例化4.为对象定制方法,save和get_obj_by_id,save能自动将对象序列化到文件中,文件路径为配置文件中DB_PATH,文件名为id号,保存之前验证对象是否已经存在,若存在则抛出异常;get_obj_by_id方法用来从文件中反序列化

数据访问,使用mysql类访问数据

数据访问分为三种 1.使用函数 在新版本里面废弃了 2.面向对象的方式 Mysqli类 3.PDO的方式 例子 <table width="100%" border="1">    <tr>        <td>代号</td>        <td>姓名</td>        <td>性别</td>        <td>民族</td>    

使用MySQL类进行数据访问

数据访问分为三种 1.使用函数 在新版本里面废弃了 2.面向对象的方式 Mysqli类 3.PDO的方式 例子 <table width="100%" border="1"> <tr> <td>代号</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>生日</td> </tr>

PHP Mysql类【转】

前几天没事在网上转发现了一个类,记录下来: <?php Class DB { private $link_id; private $handle; private $is_log; private $time; //构造函数 public function __construct() { $this->time = $this->microtime_float(); require_once("config.db.php"); $this->connect($d

非常完整的PHP的mysql类

非常完整的PHP的MySQL操作类, 即使PDO, ActiveRecord, ORM, 框架, Framework… 都不如这个强大和好用. 有了它, 你就不会再需要任何MySQL封装了,此类已经在多个大型项目中得到广泛应用, 经过生产环境至少2年的检验. 现在就来看看这个类吧! <?php class Mysql{ var $conn; var $query_list = array(); public $query_count = 0; public function __construc

php 单例模式封装MySQL类

class MysqlConn { //定义一个私有的静态属性,用来存放实例化的对象 private static $dbcon; //定义一个私有的静态属性,用来存在数据库的连接 private static $conn; //定义一个私有的解构函数,进行数据库的连接 private function __construct() { self::$conn = mysqli_connect($host,$username,$password,$dbname) or die('链接失败:'.my

python编写mysql类实现mysql的操作

前言 我们都知道利用python实现mysql的操作是件很简单的事情,只需要熟练使用MySQLdb模块就能实现mysql的增删改查操作. 为了更好地整合mysql的操作,使用python的类讲mysql的操作整合到一起,是个不错的思路.这里我编写了一个简单的class,来实现对mysql的操作与查询. 操作       本例中,我们准备在mysql的iceny中创建了一张测试表t1,字段为id和timestamp,主要存储系统的时间戳,并在该表中进行增.删.改.查的操作: 当前mysql的状态: