dbconn.php
<?php class DBConn{ private static $servername = "localhost"; private static $username = "root"; private static $password = "root"; private static $conn = null; function __construct(){ try{ self::$conn = new PDO("mysql:host=".self::$servername.";dbname=aa", self::$username, self::$password); echo "conn"; }catch(PDOException $e){ echo $e->getMessage(); } } public static function getInstance(){ if(self::$conn == null){ new DBConn(); } return self::$conn; } //查询 public function query($sql, $param){ try{ $conn = self::getInstance(); $stmt = $conn->prepare("select * from user limit :a,:b"); //$rows = $conn->query("select * from user limit 1,2")->fetchAll(PDO::FETCH_ASSOC); $param = array("a"=>1,"b"=>2); //$a = 1; //$b = 2; //$stmt->bindValue(":a", $a, PDO::PARAM_INT); //$stmt->bindValue(":b", $b, PDO::PARAM_INT); $this->bindArrayValue($stmt, $param); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); print_r($rows); }catch(PDOException $e){ print $e->getMessage(); } } public function insert($sql, $param){ try{ $conn = self::getInstance(); $stmt = $conn->prepare("select * from user limit :a,:b"); //$rows = $conn->query("select * from user limit 1,2")->fetchAll(PDO::FETCH_ASSOC); $param = array("a"=>1,"b"=>2); }catch(PDOException $e){ print $e->getMessage(); } } public function execParams($sql, $params) { $conn = self::getInstance(); $stmt = $conn->prepare("update user set password = ? and name = ?"); $result = false; if( $stm && $stm->execute(array(4, 6)) ) { $result = $stm->rowCount(); while( $stm->fetch(PDO::FETCH_ASSOC) ) { print_r $stm->fetch(PDO::FETCH_ASSOC); } } return $result; } //绑定数据类型 public function bindArrayValue($stmt, $array, $typeArray = false){ if(is_object($stmt) && ($stmt instanceof PDOStatement)){ foreach($array as $key => $value){ if($typeArray){ $stmt->bindValue(":$key",$value,$typeArray[$key]); }else{ if(is_int($value)) $param = PDO::PARAM_INT; elseif(is_bool($value)) $param = PDO::PARAM_BOOL; elseif(is_null($value)) $param = PDO::PARAM_NULL; elseif(is_string($value)) $param = PDO::PARAM_STR; else $param = FALSE; if($param) $stmt->bindValue(":$key",$value,$param); } } } } } ?>
dbdelete.php
dbhandle.php
<?php header('content-type:text/html;charset=utf-8'); try{ $conn = new PDO("mysql:host=127.0.0.1;dbname=aa","root","root"); $rows = $conn->query('select * from user')->fetchAll(PDO::FETCH_OBJ); $rs = array(); foreach($rows as $row){ $rs[] = $row; } $db = null; }catch(PDOException $e){ print $e->getMessage(); die(); } print_r($rs); print "<br/>"; try{ $conn = new PDO("mysql:host=127.0.0.1;dbname=aa","root","root"); $stmt = $conn->prepare("select * from user limit :a,:b"); $param = array("a"=>0,"b"=>2); bindArrayValue($stmt, $param); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); print_r($rows); }catch(PDOException $e){ print $e->getMessage(); } function bindArrayValue($req, $array, $typeArray = false) { if(is_object($req) && ($req instanceof PDOStatement)) { foreach($array as $key => $value) { if($typeArray) $req->bindValue(":$key",$value,$typeArray[$key]); else { if(is_int($value)) $param = PDO::PARAM_INT; elseif(is_bool($value)) $param = PDO::PARAM_BOOL; elseif(is_null($value)) $param = PDO::PARAM_NULL; elseif(is_string($value)) $param = PDO::PARAM_STR; else $param = FALSE; if($param) $req->bindValue(":$key",$value,$param); } } } } ?>
dbinsert.php
<?php include "dbconn.php"; class UserDao{ public function insert(){ $conn = DBConn::getInstance(); $sql = "insert into user(id, name, password, address) values('2','2','2','2')"; $conn->exec($sql); echo "</br>insert"; } } $userDao = new UserDao(); $userDao->insert(); ?>
dbtest.php
<?php require_once("dbconn.php"); $dbhelper = new DBConn(); $dbhelper->query(); $dbhelper->execParams("sql", $data); ?>
dbupdate.php
<?php include "dbconn.php"; class UserService{ public function update(){ $conn = DBConn::getInstance(); $sql = "update user set name=3 where id=1"; $conn->exec($sql); echo "</br>update"; } } $userService = new UserService(); $userService->update(); ?>