search
Homephp教程php手册PDO 常用类库

PDO 常用类库

Jun 13, 2016 am 10:46 AM
classpdophppublicstaticCommonly useddatabaseClass libraryconnect

1、Db.class.php

// 连接数据库  
class Db { 
    static public function getDB() { 
        try { 
            $pdo = new PDO(DB_DSN, DB_USER, DB_PWD); 
            $pdo->setAttribute(PDO::ATTR_PERSISTENT, true);  // 设置数据库连接为持久连接  
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // 设置抛出错误  
            $pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true);  // 设置当字符串为空转换为 SQL 的 NULL  
            $pdo->query('SET NAMES utf8');  // 设置数据库编码  
        } catch (PDOException $e) { 
            exit('数据库连接错误,错误信息:'. $e->getMessage()); 
        } 
        return $pdo; 
    } 

?> 
// 连接数据库
class Db {
 static public function getDB() {
  try {
   $pdo = new PDO(DB_DSN, DB_USER, DB_PWD);
   $pdo->setAttribute(PDO::ATTR_PERSISTENT, true); // 设置数据库连接为持久连接
   $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  // 设置抛出错误
   $pdo->setAttribute(PDO::ATTR_ORACLE_NULLS, true);  // 设置当字符串为空转换为 SQL 的 NULL
   $pdo->query('SET NAMES utf8');  // 设置数据库编码
  } catch (PDOException $e) {
   exit('数据库连接错误,错误信息:'. $e->getMessage());
  }
  return $pdo;
 }
}
?>
2、Model.class.php


// 操作 SQL  
class Model { 
    /**
     * SQL 增删改操作,返回受影响的行数
     * @param string $sql
     * @return int
     */ 
    public function aud($sql) { 
        try { 
            $pdo = Db::getDB(); 
            $row = $pdo->exec($sql); 
        } catch (PDOException $e) { 
            exit($e->getMessage()); 
        } 
        return $row; 
    } 
     
    /**
     * 返回全部数据,返回 PDOStatement 对象
     * @param string $sql
     * @return PDOStatement
     */ 
    public function getAll($sql) { 
        try { 
            $pdo = Db::getDB(); 
            $result = $pdo->query($sql); 
            return $result; 
        } catch (PDOException $e) { 
            exit($e->getMessage()); 
        } 
    } 

?> 


摘自 Lee.的专栏
Statement
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

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!