Home php教程 PHP源码 实用PDO MYSQL数库操作类

实用PDO MYSQL数库操作类

May 25, 2016 pm 05:13 PM
pdo

配制 

<?php
/*配制*/
return array (
&#39;DB_Default&#39;=>&#39;Default&#39;,//默认连接
/* 主数据 */
&#39;Default&#39; => 
array (
&#39;DB_HOST&#39; => &#39;127.0.0.1&#39;, // 服务器地址
&#39;DB_NAME&#39; => &#39;8now_invoicing&#39;, // 数据库名
&#39;DB_USER&#39; => &#39;root&#39;, // 用户名
&#39;DB_PWD&#39; => &#39;&#39;, // 密码
&#39;DB_PREFIX&#39; => &#39;8now_&#39;, // 数据库表前缀
&#39;DB_CHARSET&#39; => &#39;utf8&#39; ), // 数据库编码默认采用utf8
);
Copy after login
<?php
/**
 * 数据库操作类
 * @author 角度 QQ:1286522207
 *
 */
 
class db {
    public $data_Default;
    private $DB_HOST;
    private $DB_NAME;
    private $DB_USER;
    private $DB_PWD;
    private $DB_PREFIX;
    private $DB_CHARSET;
    public $Limit = " LIMIT 1";
    public $OrderBy;
    public $and;
    public $dbname;
    protected $sign = array (&#39;=&#39;, &#39;<>&#39;, &#39;>&#39;, &#39;<&#39;, &#39;>=&#39;, &#39;<=&#39; );
    function __construct($db) {
        $dbmysql = include ROOT_CONFIG . &#39;db.php&#39;;
        $dbconfig = $dbmysql [$db];
        $this->DB_HOST = $dbconfig [&#39;DB_HOST&#39;];
        $this->DB_NAME = $dbconfig [&#39;DB_NAME&#39;];
        $this->DB_USER = $dbconfig [&#39;DB_USER&#39;];
        $this->DB_PWD = $dbconfig [&#39;DB_PWD&#39;];
        $this->DB_PREFIX = $dbconfig [&#39;DB_PREFIX&#39;];
        $this->DB_CHARSET = $dbconfig [&#39;DB_CHARSET&#39;];
    }
    private function pdo_db() {
        try {
            return new PDO ( "mysql:host=$this->DB_HOST;dbname=$this->DB_NAME", $this->DB_USER, 
            $this->DB_PWD, array (PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES &#39;$this->DB_CHARSET&#39;",
             PDO::ATTR_PERSISTENT => true ) );
        } catch ( Exception $e ) {
            echo &#39;数据库连接失败,详情: &#39; . $e->getMessage () . &#39; 请在/config/db.php中配制正确的数据库连接信息&#39;;
            exit ();
        }
     
    }
    public function M($name) {
        $this->dbname = $this->DB_PREFIX . $name;
    }
    /**
     * 处理条件
     */
    public function where($where) {
        if (! $this->and)
            $this->and = &#39; and&#39;;
        if (is_array ( $where )) {
            foreach ( $where as $key => $row ) {
                $w [] = " `$key`=&#39;$row&#39;";
            }
            $sql = " WHERE (" . trim ( implode ( $this->and, $w ) ) . ") ";
        } elseif (! $where) {
            return false;
        } else {
            $sql = " WHERE " . $where;
        }
        return $sql;
    }
    /**
     * 处理数据
     */
    private function date($date) {
        if (is_array ( $date )) {
            foreach ( $date as $key => $row ) {
                $d [] = "`" . $key . "`=&#39;" . $row . "&#39;";
            }
            return implode ( &#39;,&#39;, $d );
        } else {
            return $date;
        }
    }
    /**
     * 处理插入数据
     */
    public function Upvalues($date) {
        if (is_array ( $date )) {
            foreach ( $date as $key => $row ) {
                $k [] = "`" . $key . "`";
                $r [] = "&#39;" . $row . "&#39;";
            }
            $k = "(" . implode ( &#39;,&#39;, $k ) . ")";
            $r = "(" . implode ( &#39;,&#39;, $r ) . ")";
            return $k . &#39; VALUES &#39; . $r;
        } else {
            return false;
        }
    }
    /**
     * 排序
     * @param 列 $Row
     * @param ASC or DESC$ORDER
     */
    public function OrderBy($Row = &#39;&#39;, $ORDER = &#39;DESC&#39;) {
        $this->OrderBy = "ORDER BY `$Row` $ORDER";
    }
    /**
     * 执行查询语句 SELECT
     */
    public function query($sql) {
        return empty ( $sql ) ? exit ( &#39;必须指定SQL语句&#39; ) : $this->pdo_db ()->query ( $sql );
    }
    /**
     * 执行 INSERT、UPDATE、DELETE
     */
    public function exec($sql) {
        return empty ( $sql ) ? exit ( &#39;必须指定SQL语句&#39; ) : $this->pdo_db ()->exec ( $sql );
    }
    /**
     * 查询个数
     */
    public function Limit($a = NULL, $b = NULL) {
        $this->Limit = Filter::IntegerFilter ( $a ) && Filter::IntegerFilter ( $b ) ? " LIMIT $a,$b" : &#39;&#39;;
    }
    /**
     * 处理多个查询条件
     * 
     */
    public function LIKEMultipleQueries($LIKE, $W) {
        $LIKE = array_filter ( explode ( &#39; &#39;, trim ( $LIKE ) ) );
        if (is_array ( $LIKE )) {
            foreach ( $LIKE as $row ) {
                $l [] = "`$W` LIKE  &#39;%" . trim ( $row ) . "%&#39;";
            }
            return &#39;(&#39; . implode ( &#39; OR &#39;, $l ) . &#39;)&#39;;
        }
    }
    /**
     * 点亮关键字
     */
    public $SearchLightKey = array ();
    public function SearchLightKey($array, $q) {
        foreach ( $array as $s ) {
            foreach ( $q as $key => $row ) {
                $row = array_filter ( explode ( &#39; &#39;, trim ( $row ) ) );
                if ($s [$key]) {
                    foreach ( $row as $r ) {
                        $s [$key] = preg_replace ( "/(" . trim ( $r ) . ")/i", 
                        "<font style=&#39;color: #FF9900&#39;>\\1</font>", $s [$key] );
                    }
                }
            }
            $this->SearchLightKey [] = $s;
        }
    }
    /**快捷操作开始**/
    /**
     * 查询一条数据
     */
    public function find($where, $Row = &#39;*&#39;) {
        $sql = "SELECT $Row FROM `$this->dbname` " . $this->where ( $where ) . $this->OrderBy . $this->Limit;
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 查询多条数据
     */
    public function findAll($where, $Row = &#39;*&#39;) {
        $sql = "SELECT $Row FROM `$this->dbname` " . $this->where ( $where ) . $this->OrderBy . $this->Limit;
        return $this->pdo_db ()->query ( $sql )->fetchAll ();
    }
    /**
     * 查询总数
     */
    public function findsum($where, $Row) {
        $sql = "SELECT SUM($Row) as $Row FROM `$this->dbname` " . $this->where ( $where ) . " LIMIT 1";
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 查询个数
     */
    public function findcount($where, $Row) {
        $sql = "SELECT COUNT($Row) as $Row FROM `$this->dbname` " . $this->where ( $where ) . " LIMIT 1 ";
        return $this->pdo_db ()->query ( $sql )->fetch ();
    }
    /**
     * 更新数据
     */
    public function update($date, $where) {
        $sql = "UPDATE `$this->dbname` SET " . $this->date ( $date ) . $this->where ( $where ) . ";";
        return $this->pdo_db ()->exec ( $sql );
    }
    /**
     * 插入数据
     */
    public function insert($date) {
        $sql = "INSERT INTO `$this->dbname` " . $this->Upvalues ( $date ) . ";";
        $pdo = $this->pdo_db ();
        return $pdo->exec ( $sql ) ? $pdo->lastInsertId () : false;
    }
    /**
     * 删除数据
     */
    public function delete($where) {
        $sql = "DELETE FROM `$this->dbname` " . $this->where ( $where ) . $this->Limit . ";";
        return $this->pdo_db ()->exec ( $sql );
    }
}
Copy after login

 以上就是实用PDO MYSQL数库操作类的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Solution to PHP Fatal error: Call to undefined method PDO::prepare() in Jun 22, 2023 pm 06:40 PM

PHP is a popular web development language that has been used for a long time. The PDO (PHP Data Object) class integrated in PHP is a common way for us to interact with the database during the development of web applications. However, a problem that some PHP developers often encounter is that when using the PDO class to interact with the database, they receive an error like this: PHPFatalerror:CalltoundefinedmethodPDO::prep

How to use PHP's PDO_PGSQL extension? How to use PHP's PDO_PGSQL extension? Jun 02, 2023 pm 06:10 PM

As a popular programming language, PHP is widely used in the field of web development. Among them, PHP's PDO_PGSQL extension is a commonly used PHP extension. It provides an interactive interface with the PostgreSQL database and can realize data transmission and interaction between PHP and PostgreSQL. This article will introduce in detail how to use PHP's PDO_PGSQL extension. 1. What is the PDO_PGSQL extension? PDO_PGSQL is an extension library of PHP, which

PHP and PDO: How to perform bulk inserts and updates PHP and PDO: How to perform bulk inserts and updates Jul 28, 2023 pm 07:41 PM

PHP and PDO: How to perform batch inserts and updates Introduction: When using PHP to write database-related applications, you often encounter situations where you need to batch insert and update data. The traditional approach is to use loops to perform multiple database operations, but this method is inefficient. PHP's PDO (PHPDataObject) provides a more efficient way to perform batch insert and update operations. This article will introduce how to use PDO to implement batch insert and update operations. 1. Introduction to PDO: PDO is PH

PHP and PDO: How to perform paging queries and display data PHP and PDO: How to perform paging queries and display data Jul 29, 2023 pm 04:10 PM

PHP and PDO: How to query and display data in pages When developing web applications, querying and displaying data in pages is a very common requirement. Through paging, we can display a certain amount of data at a time, improving page loading speed and user experience. In PHP, the functions of paging query and display of data can be easily realized using the PHP Data Object (PDO) library. This article will introduce how to use PDO in PHP to query and display data by page, and provide corresponding code examples. 1. Create database and data tables

PHP and PDO: How to handle JSON data in a database PHP and PDO: How to handle JSON data in a database Jul 29, 2023 pm 05:17 PM

PHP and PDO: How to handle JSON data in databases In modern web development, processing and storing large amounts of data is a very important task. With the popularity of mobile applications and cloud computing, more and more data are stored in databases in JSON (JavaScript Object Notation) format. As a commonly used server-side language, PHP's PDO (PHPDataObject) extension provides a convenient way to process and operate databases. Book

PHP and PDO: How to perform a full-text search in a database PHP and PDO: How to perform a full-text search in a database Jul 30, 2023 pm 04:33 PM

PHP and PDO: How to perform a full-text search in a database In modern web applications, the database is a very important component. Full-text search is a very useful feature when we need to search for specific information from large amounts of data. PHP and PDO (PHPDataObjects) provide a simple yet powerful way to perform full-text searches in databases. This article will introduce how to use PHP and PDO to implement full-text search, and provide some sample code to demonstrate the process. first

PHP PDO vs. mysqli: compare and contrast PHP PDO vs. mysqli: compare and contrast Feb 19, 2024 pm 12:24 PM

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

PHP PDO Tutorial: An Advanced Guide from Basics to Mastery PHP PDO Tutorial: An Advanced Guide from Basics to Mastery Feb 19, 2024 pm 06:30 PM

1. Introduction to PDO PDO is an extension library of PHP, which provides an object-oriented way to operate the database. PDO supports a variety of databases, including Mysql, postgresql, oracle, SQLServer, etc. PDO enables developers to use a unified API to operate different databases, which allows developers to easily switch between different databases. 2. PDO connects to the database. To use PDO to connect to the database, you first need to create a PDO object. The constructor of the PDO object receives three parameters: database type, host name, database username and password. For example, the following code creates an object that connects to a mysql database: $dsn="mysq

See all articles