Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. class db {
  2. public static $dsn;
  3. public static $username;
  4. public static $password;
  5. public static $db;
  6. public static $conn;
  7.  
  8. public static $arrConnAttr;//Array of connection attributes. Ex: ["PDO::ATTR_ERRMODE", "PDO::ERRMODE_EXCEPTION"]
  9. public static $pathErroLog;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
  10. public static $arrCatchConnResult;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
  11. public static $die;//If we set $setValues["die"]=NULL, during the object initialization, errors will not stop the process.
  12.  
  13. public static $sql;
  14.  
  15. private static function init(array $setValues=NULL) {
  16.  
  17.  
  18. $arrInitDefault["dsn"]="localhost";
  19. $arrInitDefault["username"]= "root";
  20. $arrInitDefault["password"]="";
  21. $arrInitDefault["db"]="drset2014";
  22.  
  23.  
  24.  
  25. $arrInitDefault["arrConnAttr"]= ["PDO::ATTR_ERRMODE"," PDO::ERRMODE_EXCEPTION"];
  26.  
  27. $arrInitDefault["pathErroLog"]= dirname(__FILE__)."/Log/Error";//Default path to dir. Note: error pathLog can be "/Log/Error/UserName" or "/Log/Error/UserId", etc
  28. $arrInitDefault["die"]= TRUE;
  29.  
  30. $arrInitDefault["sql"]= FALSE;
  31.  
  32.  
  33. if(!is_null($setValues)){
  34. $arrInitDefault= array_merge($arrInitDefault,$setValues);
  35. }
  36.  
  37. //After merge, initialaze private variables with result merged $arrValues
  38.  
  39. self::$dsn = $arrInitDefault["dsn"];
  40. self::$username= $arrInitDefault["username"];
  41. self::$password= $arrInitDefault["password"];
  42. self::$db= $arrInitDefault["db"];
  43.  
  44. self::$arrConnAttr= implode(",", $arrInitDefault["arrConnAttr"]);
  45. self::$pathErroLog=$arrInitDefault["pathErroLog"];
  46. self::$die=$arrInitDefault["die"];
  47. //This try and catch is only to estabilish the connection during the initialization
  48. try {
  49.  
  50. $dns=self::$dsn;
  51. $username=self::$username;
  52. $password=self::$password;
  53. $db=self::$db;
  54.  
  55. self::$conn= new PDO("mysql:host=$dns; dbname=$db", "$username", "$password");//Now private $conn is a PDO object
  56. self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  57.  
  58. }// End of try
  59. catch(PDOException $e){//If had some error. The PDO object ($this->conn) could not be created. Throw an error.
  60. self::$arrCatchConnResult = self::saveLogError($e->getMessage());//Save a message in the log file and throw a message
  61. $msg=self::$arrCatchConnResult["strToHTML"];
  62. self::$conn=null;
  63. if(self::$die){
  64. die($msg);
  65. }
  66. }
  67. }
  68.  
  69. public static function saveLogError($msg=NULL,$sql=NULL) {
  70.  
  71. $sqlErrorStringHTML="";
  72.  
  73. $year = date("Y");
  74. $month = date("M");
  75. $day = date("d");
  76. $dt = date("d.M.Y_H.i.s");
  77.  
  78. $pathErroLog= self::$pathErroLog."/".$year."/".$month."/".$day."/";
  79. if (!is_dir($pathErroLog)) {
  80. mkdir($pathErroLog,0777,true);
  81. }//If is not a dir, create it.
  82.  
  83. $sqlErrorStringTXT="";
  84. $sqlErrorString="";
  85. if(!is_null($sql)){
  86. $sqlErrorStringTXT = "Sql: $sql";
  87. $sqlErrorStringHTML = "<kbd>Sql</kbd>: $sql";
  88. }
  89.  
  90. /////////////// Maybe should be good save errors in database too.
  91.  
  92. $strToTxt=<<<EOF
  93. >>>>> Mensagem de erro: <<<<< \n
  94. Date: $dt \n
  95. Msg: $msg \n
  96. $sqlErrorStringTXT \n
  97. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  98. EOF;
  99.  
  100. //Save error message to the file
  101. $f=$pathErroLog."$dt.txt";
  102. $myfile = fopen($f, "w") or die("Unable to open file!");
  103. fwrite($myfile, $strToTxt);
  104. fclose($myfile);
  105.  
  106. //Create a string to be displayed in the broser
  107. $strToHTML=<<<EOF
  108. <div class="alert alert-danger" role="alert">
  109. Date: $dt <br/>
  110. Msg: <span class="label label-danger">$msg</span><br/>
  111. $sqlErrorStringHTML<br/>
  112. </div>
  113. EOF;
  114.  
  115. $arrCatchConnResult["status"]=0;
  116. $arrCatchConnResult["strToTxt"]=$strToTxt;
  117. $arrCatchConnResult["strToHTML"]=$strToHTML;
  118.  
  119. return $arrCatchConnResult;
  120.  
  121. }//End of saveLogError()
  122.  
  123. public static function get_values(array $setValues=NULL) {
  124. try {
  125. self::init($setValues);
  126. $stmt = self::$conn->prepare(self::$sql);
  127. $stmt->execute();
  128. while ($field = $stmt->fetch(PDO::FETCH_ASSOC)) {
  129. $arrResult[]=$field;
  130. }
  131. return $arrResult;
  132. }
  133. catch (PDOException $e) {
  134. self::$arrCatchConnResult = self::saveLogError($e->getMessage(), self::$sql);//Save a message in the log file and set a message
  135. $msg= self::$arrCatchConnResult["strToHTML"];
  136. self::$conn = null;
  137. if(self::$die){
  138. die($msg);
  139. }
  140. }
  141. }
  142.  
  143.  
  144. }//End of class db
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement