天天看點

php pdo mysql類源碼_實用PDO MYSQL數庫操作類

配制

return array (

'DB_Default'=>'Default',//預設連接配接

'Default' =>

array (

'DB_HOST' => '127.0.0.1', // 伺服器位址

'DB_NAME' => '8now_invoicing', // 資料庫名

'DB_USER' => 'root', // 使用者名

'DB_PWD' => '', // 密碼

'DB_PREFIX' => '8now_', // 資料庫表字首

'DB_CHARSET' => 'utf8' ), // 資料庫編碼預設采用utf8

);<?php

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 ('=', '<>', '>', '=', '<=' );

function __construct($db) {

$dbmysql = include ROOT_CONFIG . 'db.php';

$dbconfig = $dbmysql [$db];

$this->DB_HOST = $dbconfig ['DB_HOST'];

$this->DB_NAME = $dbconfig ['DB_NAME'];

$this->DB_USER = $dbconfig ['DB_USER'];

$this->DB_PWD = $dbconfig ['DB_PWD'];

$this->DB_PREFIX = $dbconfig ['DB_PREFIX'];

$this->DB_CHARSET = $dbconfig ['DB_CHARSET'];

}

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 '$this->DB_CHARSET'",

PDO::ATTR_PERSISTENT => true ) );

} catch ( Exception $e ) {

echo '資料庫連接配接失敗,詳情: ' . $e->getMessage () . ' 請在/config/db.php中配制正确的資料庫連接配接資訊';

exit ();

}

}

public function M($name) {

$this->dbname = $this->DB_PREFIX . $name;

}

public function where($where) {

if (! $this->and)

$this->and = ' and';

if (is_array ( $where )) {

foreach ( $where as $key => $row ) {

$w [] = " `$key`='$row'";

}

$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 . "`='" . $row . "'";

}

return implode ( ',', $d );

} else {

return $date;

}

}

public function Upvalues($date) {

if (is_array ( $date )) {

foreach ( $date as $key => $row ) {

$k [] = "`" . $key . "`";

$r [] = "'" . $row . "'";

}

$k = "(" . implode ( ',', $k ) . ")";

$r = "(" . implode ( ',', $r ) . ")";

return $k . ' VALUES ' . $r;

} else {

return false;

}

}

public function OrderBy($Row = '', $ORDER = 'DESC') {

$this->OrderBy = "ORDER BY `$Row` $ORDER";

}

public function query($sql) {

return empty ( $sql ) ? exit ( '必須指定SQL語句' ) : $this->pdo_db ()->query ( $sql );

}

public function exec($sql) {

return empty ( $sql ) ? exit ( '必須指定SQL語句' ) : $this->pdo_db ()->exec ( $sql );

}

public function Limit($a = NULL, $b = NULL) {

$this->Limit = Filter::IntegerFilter ( $a ) && Filter::IntegerFilter ( $b ) ? " LIMIT $a,$b" : '';

}

public function LIKEMultipleQueries($LIKE, $W) {

$LIKE = array_filter ( explode ( ' ', trim ( $LIKE ) ) );

if (is_array ( $LIKE )) {

foreach ( $LIKE as $row ) {

$l [] = "`$W` LIKE '%" . trim ( $row ) . "%'";

}

return '(' . implode ( ' OR ', $l ) . ')';

}

}

public $SearchLightKey = array ();

public function SearchLightKey($array, $q) {

foreach ( $array as $s ) {

foreach ( $q as $key => $row ) {

$row = array_filter ( explode ( ' ', trim ( $row ) ) );

if ($s [$key]) {

foreach ( $row as $r ) {

$s [$key] = preg_replace ( "/(" . trim ( $r ) . ")/i",

"\\1", $s [$key] );

}

}

}

$this->SearchLightKey [] = $s;

}

}

public function find($where, $Row = '*') {

$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 = '*') {

$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 );

}

}

以上就是實用PDO MYSQL數庫操作類的内容,更多相關内容請關注PHP中文網(www.php.cn)!

本文原創釋出php中文網,轉載請注明出處,感謝您的尊重!