Heim > php教程 > php手册 > php mysql 类

php mysql 类

WBOY
Freigeben: 2016-06-06 19:35:30
Original
1502 Leute haben es durchsucht

private mixed $mysql-bindValue private string $mysql-defaultDB private string $mysql-sql public int $mysql-queries public int $mysql-insert_id public int $mysql-num_rows public int $mysql-affected_rows mysql::__construct([array$argv=array()

private mixed $mysql->bindValue
private string $mysql->defaultDB
private string $mysql->sql

public int $mysql->queries
public int $mysql->insert_id
public int $mysql->num_rows
public int $mysql->affected_rows


mysql::__construct( [ array $argv = array() ] );
mysql::connect( array $argv, [ string $charset = null ] );
mysql::selectDB( string $database );
mysql::query( string $sql );
mysql::find( string $sql );
mysql::findOne( string $sql );
mysql::setCharset( string $charset );
mysql::ping( void );
mysql::beginTransaction( void );
mysql::commit( void );
mysql::rollback( void );
mysql::prepare ( string $sql );
mysql::bindValue ( mixed $search, mixed $value );
mysql::execute( [ array $argv = array() ] );
mysql::escape( string $sql );
mysql::close( void );
mysql::__destruct( void );

待续 ...
<?php

class mysql {
       
    private $defaultDB = null;
    private $link = null;
    private $sql = null;
	private $host_info = null;
    private $bindValue = null;
    public $num_rows = 0;
    public $affected_rows = 0;
    public $insert_id = 0;
    public $queries = 0;
       
    public function __construct() {
         
        if(func_num_args()) {
            $argv = func_get_arg(0);
            if(!empty($argv) && is_array($argv)) { 
                $this->connect($argv);
                $argv['charset'] = isset($argv['charset']) ? $argv['charset'] : 'utf8';
                $this->setCharset($argv['charset']);
            }
        }
          
    }
       
    public function connect($argv, $charset = null) {
           
        if($this->link) return false;
        $argv = func_get_arg(0);
        $argv['port'] = isset($argv['port']) ? $argv['port'] : 3306;
        $this->link = mysqli_connect( $argv['host'], $argv['user'], $argv['password'], $argv['database'], $argv['port']);
        if(mysqli_connect_errno()) {
			echo mysqli_connect_error(); 
			exit(0);
		}
		
        $this->defaultDB = $argv['database'];
		$this->host_info = (object) $argv;
          
        if($charset) $this->setCharset($charset);
    }
       
    public function selectDB($database){
       
        $int = mysqli_select_db($this->link, $database);
        if($int) $this->defaultDB = $database;
        return $int;
           
    }
       
    public function query($sql) {
       
        $result = mysqli_query($this->link, $sql);
        if(mysqli_errno($this->link)) {
			echo mysqli_error($this->link);
			exit(0);
		}
		
        $this->queries++;
		
        if(preg_match('/^use\s+(\w+)/', $sql, $matches))
		list($range, $this->defaultDB) = $matches;
		
        if(!preg_match('/^select(.+)$/i', $sql)) {
            $this->affected_rows = mysqli_affected_rows($this->link);
        }else{
            $this->num_rows = mysqli_num_rows($result);
        }
           
        if(preg_match('/^insert(.+)$/i', $sql))
		$this->insert_id = mysqli_insert_id($this->link);
           
        return $result;
           
    }
       
    public function find($sql) {
       
        $collection = array();
        $result = $this->query($sql);
        while($rows = mysqli_fetch_assoc($result))
        array_push($collection, $rows);
        mysqli_free_result($result);
        return $collection;
           
    }
       
    public function findOne($sql) {
       
        $result = $this->query($sql);
        $rows = mysqli_fetch_assoc($result);
        mysqli_free_result($result);
        return $rows;
           
    }
       
    public function setCharset($charset) {
       
        return mysqli_set_charset($this->link, $charset);
           
    }
     
    public function prepare($sql) {
     
        $this->sql = $sql;
         
    }
     
    public function bindValue($search, $value) {
         
        $this->bindValue = array();
        $this->bindValue[$search] = $value;
         
    }
     
    public function execute() {
     
        if(func_num_args()) {
            $argv = func_get_arg(0);
            if(!empty($argv) && is_array($argv)) {
                if(!is_array($this->bindValue)) $this->bindValue = array();
                $this->bindValue = array_merge($this->bindValue, $argv);
            }
        }
         
        if($this->bindValue) {
            foreach($this->bindValue as $search => $value) {
                $this->sql = str_replace($search, $this->escape($value), $this->sql);
            }
            $this->bindValue = null;
        }
         
        $int = $this->query($this->sql);
        //$this->sql = null;
        return (boolean) $int;
    }
     
     
    public function escape($string) {
     
        return mysqli_real_escape_string($this->link, $string);
     
    }
     
    public function close() {
         
        return mysqli_close($this->link);
         
    }
       
    public function ping() {
       
        return mysqli_ping($this->link);
           
    }
       
    public function beginTransaction($boolean) {
       
        return mysqli_autocommit($this->link, $boolean);
           
    }
       
    public function commit() {
       
        return mysqli_commit($this->link);
           
    }
       
    public function rollback() {
       
        return mysqli_rollback($this->link);
           
    }
       
    public function __destruct() {
       
        if($this->link) $this->close();
        unset($this->link, $this->defaultDB, $this->bindValue, $this->sql, $this->result, $this->num_rows, $this->affected_rows, $this->insert_id, $this->host_info);
           
    }
       
}

$argv = array(
'host' => 'localhost',
'user' => 'root',
'password' => '',
'port' => 3306,
'database' => 'test',
'charset'=> 'utf8');


// Using the "mysql::__construct" method to connect MySQL database

$mysql = new mysql($argv);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);


// Using the "mysql::connect" method to connect MySQL database

$mysql = new mysql();
$mysql->connect($argv);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);


$mysql = new mysql();
$mysql->connect($argv);
$mysql->setCharset($argv['charset']);
var_dump($mysql->find('select version()'));
var_dump($mysql->queries);
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage