Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class db {
- public static $dsn;
- public static $username;
- public static $password;
- public static $db;
- public static $conn;
- public static $arrConnAttr;//Array of connection attributes. Ex: ["PDO::ATTR_ERRMODE", "PDO::ERRMODE_EXCEPTION"]
- public static $pathErroLog;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
- public static $arrCatchConnResult;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
- public static $die;//If we set $setValues["die"]=NULL, during the object initialization, errors will not stop the process.
- public static $sql;
- private static function init(array $setValues=NULL) {
- $arrInitDefault["dsn"]="localhost";
- $arrInitDefault["username"]= "root";
- $arrInitDefault["password"]="";
- $arrInitDefault["db"]="drset2014";
- $arrInitDefault["arrConnAttr"]= ["PDO::ATTR_ERRMODE"," PDO::ERRMODE_EXCEPTION"];
- $arrInitDefault["pathErroLog"]= dirname(__FILE__)."/Log/Error";//Default path to dir. Note: error pathLog can be "/Log/Error/UserName" or "/Log/Error/UserId", etc
- $arrInitDefault["die"]= TRUE;
- $arrInitDefault["sql"]= FALSE;
- if(!is_null($setValues)){
- $arrInitDefault= array_merge($arrInitDefault,$setValues);
- }
- //After merge, initialaze private variables with result merged $arrValues
- self::$dsn = $arrInitDefault["dsn"];
- self::$username= $arrInitDefault["username"];
- self::$password= $arrInitDefault["password"];
- self::$db= $arrInitDefault["db"];
- self::$arrConnAttr= implode(",", $arrInitDefault["arrConnAttr"]);
- self::$pathErroLog=$arrInitDefault["pathErroLog"];
- self::$die=$arrInitDefault["die"];
- //This try and catch is only to estabilish the connection during the initialization
- try {
- $dns=self::$dsn;
- $username=self::$username;
- $password=self::$password;
- $db=self::$db;
- self::$conn= new PDO("mysql:host=$dns; dbname=$db", "$username", "$password");//Now private $conn is a PDO object
- self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }// End of try
- catch(PDOException $e){//If had some error. The PDO object ($this->conn) could not be created. Throw an error.
- self::$arrCatchConnResult = self::saveLogError($e->getMessage());//Save a message in the log file and throw a message
- $msg=self::$arrCatchConnResult["strToHTML"];
- self::$conn=null;
- if(self::$die){
- die($msg);
- }
- }
- }
- public static function saveLogError($msg=NULL,$sql=NULL) {
- $sqlErrorStringHTML="";
- $year = date("Y");
- $month = date("M");
- $day = date("d");
- $dt = date("d.M.Y_H.i.s");
- $pathErroLog= self::$pathErroLog."/".$year."/".$month."/".$day."/";
- if (!is_dir($pathErroLog)) {
- mkdir($pathErroLog,0777,true);
- }//If is not a dir, create it.
- $sqlErrorStringTXT="";
- $sqlErrorString="";
- if(!is_null($sql)){
- $sqlErrorStringTXT = "Sql: $sql";
- $sqlErrorStringHTML = "<kbd>Sql</kbd>: $sql";
- }
- /////////////// Maybe should be good save errors in database too.
- $strToTxt=<<<EOF
- >>>>> Mensagem de erro: <<<<< \n
- Date: $dt \n
- Msg: $msg \n
- $sqlErrorStringTXT \n
- >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
- EOF;
- //Save error message to the file
- $f=$pathErroLog."$dt.txt";
- $myfile = fopen($f, "w") or die("Unable to open file!");
- fwrite($myfile, $strToTxt);
- fclose($myfile);
- //Create a string to be displayed in the broser
- $strToHTML=<<<EOF
- <div class="alert alert-danger" role="alert">
- Date: $dt <br/>
- Msg: <span class="label label-danger">$msg</span><br/>
- $sqlErrorStringHTML<br/>
- </div>
- EOF;
- $arrCatchConnResult["status"]=0;
- $arrCatchConnResult["strToTxt"]=$strToTxt;
- $arrCatchConnResult["strToHTML"]=$strToHTML;
- return $arrCatchConnResult;
- }//End of saveLogError()
- public static function get_values(array $setValues=NULL) {
- try {
- self::init($setValues);
- $stmt = self::$conn->prepare(self::$sql);
- $stmt->execute();
- while ($field = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $arrResult[]=$field;
- }
- return $arrResult;
- }
- catch (PDOException $e) {
- self::$arrCatchConnResult = self::saveLogError($e->getMessage(), self::$sql);//Save a message in the log file and set a message
- $msg= self::$arrCatchConnResult["strToHTML"];
- self::$conn = null;
- if(self::$die){
- die($msg);
- }
- }
- }
- }//End of class db
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement