为了让自己的数据类能够做到最大化的重用,就写个能够重用的PDO操作MySql的类,由于pdo可以连接现在流行的各种数据库,所以单独的写个配置类类来完成不同数据库DSN的配置,PDO操作MYSQL类代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class ConfigDataBase {
-
- protected static $_dbms = "mysql";
- protected static $_host = '127.0.0.1';
- protected static $_port = '3306';
- protected static $_username = 'root';
- protected static $_password = 'liujijun';
- protected static $_dbname = 'zendf';
- protected static $_charset = 'utf-8';
- protected static $_dsn;
-
-
-
-
- public static function getDsn() {
-
- if (!isset(self::$_dsn)){
- self::$_dsn = self::$_dbms.':host = '.self::$_host.';prot = '.
- self::$_port . ';dbname = ' . self::$_dbname.','.
- self::$_username . ','.self::$_password;
-
- if (strlen(self::$_charset) > 0){
- self::$_dsn = self::$_dsn . ';charset = ' . self::$_charset;
- }
- }
- return self::$_dsn;
- }
-
-
-
-
-
- public static function setDbms($dbms){
- if (isset($dbms) &&(strlen($dbms) > 0 )){
- self::$_dbms = trim($dbms);
- }
- }
-
-
-
-
-
- public static function setHost($host){
- if (isset($host) &&(strlen($host) > 0 )){
- self::$_host = trim($host);
- }
- }
-
-
-
-
-
- public static function setPort($port){
- if (isset($port) &&(strlen($port) > 0 )){
- self::$_post = trim($port);
- }
- }
-
-
-
-
-
- public static function setPasswd($passwd){
- if (isset($passwd) &&(strlen($passwd) > 0 )){
- self::$_password = trim($passwd);
- }
- }
-
-
-
-
-
- public static function setUsernName($username){
- if (isset($username) &&(strlen($username) > 0 )){
- self::$_username = trim($username);
- }
- }
-
-
-
-
-
- public static function setDbName($dbname){
- if (isset($dbname) &&(strlen($dbname) > 0 )){
- self::$_dbname = trim($dbname);
- }
- }
-
-
-
-
-
-
- public static function setCharset($charset){
- if (isset($charset) &&(strlen($charset) > 0 )){
- self::$_charset = trim($charset);
- }
- }
- }
-
- 下面是对数据库的操作:
-
-
-
- <?php
-
- require_once 'ConfigDataBase.php';
- header("Content-Type: text/html; charset=utf-8");
-
-
-
-
-
-
-
-
-
-
-
-
-
- class PdoMysqlOperater{
-
-
-
-
-
- public function getConnection(){
- $connection = NULL;
- try {
- $connection = new PDO(ConfigDataBase::getDsn());
- echo 'Success';
- } catch (PDOException $e) {
- print "Error in connection :".$e->getMessage().' '.die();
- }
- return $connection;
- }
-
-
-
-
-
- public function closeConnection($connection){
- try {
- if ($connection != null) {
- $connection = null;
- }
- } catch (Exception $e) {
- print 'Close the connectin is error:'.$e->getMessage();
- }
-
- }
-
-
-
-
-
- public function insertDatabase($sql){
- $affect = false;
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $affect = true;
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'Insert error '.$e->getMessage();
- }
- return $affect;
- }
-
-
-
-
-
-
- public function deleltById($id,$tableName){
- $affact = false;
- $sql = 'delete from '.trim($tableName).' where id = '.$id;
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- $affact = true;
- } catch (PDOException $e) {
- print 'Delelte error is '.$e->getMessage();
- }
- return $affact;
- }
-
-
-
-
-
-
- public function prepareDeleteAnd($tableName,array $array=null){
- $sql = 'delete from '. $tableName . ' where ';
- $count = count($array);
- $flag = 0;
-
- foreach ($array as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count ){
- $sql .= ' and ';
- }
- }
- echo $sql;
- try {
- $conn = $this->getConnection();
- $conn->prepare($sql);
- $this->closeConnection();
- } catch (PDOException $e) {
- print 'Delete error is '.$e->getMessage();
- }
-
- }
-
-
-
-
-
-
-
- public function prepareDeleteOr($tableName,array $array=null){
-
- $sql = 'delete from '. $tableName . ' where ';
- $count = count($array);
- $flag = 0;
-
- foreach ($array as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count ){
- $sql .= ' or ';
- }
- }
- echo $sql;
- try {
- $conn = $this->getConnection();
- $stmt = $conn->prepare($sql);
- $stmt->execute();
- $this->closeConnection();
- } catch (PDOException $e) {
- print 'Delete error is '.$e->getMessage();
- }
-
- }
-
-
-
-
-
-
- public function getAll($sql){
-
- $result = null;
- try {
- $conn = $this->getConnection();
- $result = $conn->query($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'GetAll error is '.$e->getMessage();
- }
- }
-
-
-
-
-
-
-
-
- public function updateDataBase($table,array $updateFiled,array $updateConditon ){
-
- $sql = 'update from ' .$table .' set ';
-
-
- $count = count($updateFiled);
- $flag = 0;
- foreach ($updateFiled as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count){
- $sql .=',';
- }
- }
-
- $countUpdateCondition = count($updateConditon);
- $flag = 0;
- $sql .= ' where ';
- foreach ($updateConditon as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $countUpdateCondition){
- $sql .=' and ';
- }
- }
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'Update error is :'.$e->getMessage();
- }
-
- }
-
-
-
-
-
-
-
-
- public function findData($table,array $findCondition){
-
- $sql = 'select from '.$table .' where ';
-
- $count = count($findCondition);
- $flag = 0;
- foreach ($findCondition as $key => $value){
- $flag++;
- $sql .= $key .'='."'".$value."'";
- if ($flag != $count){
- $sql .=' and ';
- }
- }
- try {
- $conn = $this->getConnection();
- $conn->exec($sql);
- $this->closeConnection($conn);
- } catch (PDOException $e) {
- print 'find error is :'.$e->getMessage();
- }
-
- }
- }
-
-
- $db = new PdoMysqlOperater();
- $db->findData('liujijun',array('name'=>'liujijun','name1'=>'liujijun'));
- ?>
|