<?php /** * Mypdo */ Class Mypdo { protected static $_instance = NULL; protected $dbName = "; protected $dsn; protected $DBH; /** * Construction * * @return Mypdo */ Private Function __construct ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset) { try { $this->dsn = ' mysql:host= '. $dbHost. '; Dbname= '. $dbName; $this->DBH = new PDO ($this->dsn, $dbUser, $DBPASSWD); $this->dbh->exec (' SET character_set_connection= '. $dbCharset. ', character_set_results= '. $dbCharset. ', Character_set_client=binary '); } <a href= "\"/tags.php/catch/\ "" target= "\" _blank\ "" >catch</a> (Pdoexception $e) { $this->outputerror ($e->getmessage ()); } } /** * Prevent cloning * */ Private Function __clone () {} /** * Singleton Instance * * @return Object */ public static function getinstance ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset) { if (self::$_instance = = = null) { Self::$_instance = new self ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset); } return self::$_instance; } /** * Query queries * * @param String $STRSQL SQL statement * @param String $queryMode Query method (all or Row) * @param Boolean $debug * @return Array */ Public Function query ($STRSQL, $queryMode = ' all ', $debug = False) { if ($debug = = = True) $this->debug ($STRSQL); $recordset = $this->dbh->query ($STRSQL); $this->getpdoerror (); if ($recordset) { $recordset->setfetchmode (PDO::FETCH_ASSOC); if ($queryMode = = ' All ') { $result = $recordset->fetchall (); } elseif ($queryMode = = ' Row ') { $result = $recordset->fetch (); } } else { $result = null; } return $result; } /** * Update Updates * * @param String $table table name * @param Array $arrayDataValue fields and values * @param String $where condition * @param Boolean $debug * @return Int */ Public Function Update ($table, $arrayDataValue, $where = ", $debug = False) { $this->checkfields ($table, $arrayDataValue); if ($where) { $STRSQL = "; <a href= "\"/tags.php/foreach/\ "" target= "\" _blank\ "" >foreach</a> ($arrayDataValue as $key = = $value) { $strSql. = ", ' $key ' = ' $value '"; } $STRSQL = <a href= "\"/tags.php/substr/\ "" target= "\" _blank\ "" >substr</a> ($STRSQL, 1); $STRSQL = "UPDATE ' $table ' SET $strSql WHERE $where"; } else { $STRSQL = "REPLACE into ' $table ' ('". Implode ("," ", Array_keys ($arrayDataValue))." ') VALUES (' ". Implode (" ', ' ", $arrayDataValue)." ') "; } if ($debug = = = True) $this->debug ($STRSQL); $result = $this->dbh->exec ($STRSQL); $this->getpdoerror (); return $result; } /** * Insert Insertion * * @param String $table table name * @param Array $arrayDataValue fields and values * @param Boolean $debug * @return Int */ Public Function Insert ($table, $arrayDataValue, $debug = False) { $this->checkfields ($table, $arrayDataValue); $STRSQL = "INSERT into ' $table ' ('". Implode ("," ", Array_keys ($arrayDataValue))." ') VALUES (' ". Implode (" ', ' ", $arrayDataValue)." ') "; if ($debug = = = True) $this->debug ($STRSQL); $result = $this->dbh->exec ($STRSQL); $this->getpdoerror (); return $result; } /** * Replace Overwrite mode insert * * @param String $table table name * @param Array $arrayDataValue fields and values * @param Boolean $debug * @return Int */ Public function replace ($table, $arrayDataValue, $debug = False) { $this->checkfields ($table, $arrayDataValue); $STRSQL = "REPLACE into ' $table ' ('". Implode ("," ", Array_keys ($arrayDataValue))." ') VALUES (' ". Implode (" ', ' ", $arrayDataValue)." ') "; if ($debug = = = True) $this->debug ($STRSQL); $result = $this->dbh->exec ($STRSQL); $this->getpdoerror (); return $result; } /** * Delete deletion * * @param String $table table name * @param String $where condition * @param Boolean $debug * @return Int */ Public Function Delete ($table, $where = ", $debug = False) { if ($where = = ") { $this->outputerror ("' WHERE ' is Null"); } else { $STRSQL = "DELETE from" $table ' WHERE $where '; if ($debug = = = True) $this->debug ($STRSQL); $result = $this->dbh->exec ($STRSQL); $this->getpdoerror (); return $result; } } /** * Execsql Execute SQL statement * * @param String $STRSQL * @param Boolean $debug * @return Int */ Public Function Execsql ($STRSQL, $debug = False) { if ($debug = = = True) $this->debug ($STRSQL); $result = $this->dbh->exec ($STRSQL); $this->getpdoerror (); return $result; } /** * Get field Maximum value * * @param string $table table name * @param string $field _name field name * @param string $where condition */ Public Function Getmaxvalue ($table, $field _name, $where = ", $debug = False) { $STRSQL = "Select MAX (". $field _name. ") As Max_value from $table "; if ($where! = ") $strSql. =" where $where "; if ($debug = = = True) $this->debug ($STRSQL); $arrTemp = $this->query ($strSql, ' Row '); $maxValue = $arrTemp ["Max_value"]; if ($maxValue = = "" | | $maxValue = = null) { $maxValue = 0; } return $maxValue; } /** * Gets the number of specified columns * * @param string $table * @param string $field _name * @param string $where * @param bool $debug * @return int */ Public Function GetCount ($table, $field _name, $where = ", $debug = False) { $STRSQL = "Select COUNT ($field _name) as NUM from $table"; if ($where! = ") $strSql. =" where $where "; if ($debug = = = True) $this->debug ($STRSQL); $arrTemp = $this->query ($strSql, ' Row '); return $arrTemp [' NUM ']; } /** * Get the Table engine * * @param String $dbName Library Name * @param String $tableName table name * @param Boolean $debug * @return String */ Public Function Gettableengine ($dbName, $tableName) { $STRSQL = "SHOW TABLE STATUS from $dbName WHERE name= '". $tableName. "'"; $arrayTableInfo = $this->query ($STRSQL); $this->getpdoerror (); return $arrayTableInfo [0][' Engine ']; } /** * BeginTransaction Transaction start */ Private Function BeginTransaction () { $this->dbh->begintransaction (); } /** * COMMIT TRANSACTION Commit */ Private Function Commit () { $this->dbh->commit (); } /** * ROLLBACK TRANSACTION rollback */ Private Function rollback () { $this->dbh->rollback (); } /** * Transaction multiple SQL statements with transactions * Before calling to determine whether the table engine supports transactions through Gettableengine * * @param array $ARRAYSQL * @return Boolean */ Public Function exectransaction ($ARRAYSQL) { $retval = 1; $this->begintransaction (); foreach ($arraySql as $STRSQL) { if ($this->execsql ($strSql) = = 0) $retval = 0; } if ($retval = = 0) { $this->rollback (); return false; } else { $this->commit (); return true; } } /** * Checkfields checks whether the specified field exists in the specified data table * * @param String $table * @param array $arrayField */ Private Function Checkfields ($table, $arrayFields) { $fields = $this->getfields ($table); foreach ($arrayFields as $key = = $value) { if (!in_array ($key, $fields)) { $this->outputerror ("Unknown column ' $key ' in field list."); } } } /** * GetFields gets all field names in the specified data table * * @param String $table table name * @return Array */ Private Function GetFields ($table) { $fields = Array (); $recordset = $this->dbh->query ("SHOW COLUMNS from $table"); $this->getpdoerror (); $recordset->setfetchmode (PDO::FETCH_ASSOC); $result = $recordset->fetchall (); foreach ($result as $rows) { $fields [] = $rows [' Field ']; } return $fields; } /** * Getpdoerror capturing PDO error messages */ Private Function Getpdoerror () { if ($this->dbh->errorcode ()! = ' 00000 ') { $arrayError = $this->dbh->errorinfo (); $this->outputerror ($arrayError [2]); } } /** * Debug * * @param mixed $debuginfo */ Private Function Debug ($debuginfo) { Var_dump ($debuginfo); Exit (); } /** * Output error message * * @param String $STRERRMSG */ Private Function Outputerror ($STRERRMSG) { throw new Exception (' MySQL Error: '. $strErrMsg); } /** * Destruct Close database connection */ Public Function Destruct () { $this->DBH = null; } } ?> |