Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Em desenvolvimento.
- * Objeto para gerenciar uma
- *conexão ao banco de dados Mysql
- * @author Igor
- */
- class db {
- private $dsn;
- private $username;
- private $password;
- private $db;
- private $conn;
- private $arrConnAttr;//Array of connection attributes. Ex: ["PDO::ATTR_ERRMODE", "PDO::ERRMODE_EXCEPTION"]
- private $pathErroLog;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
- private $arrCatchConnResult;//Path to save the erro log files. Ex:$_SERVER['DOCUMENT_ROOT'] . "/myDirRootProject/LogError"
- private $die;//If we set $setValues["die"]=NULL, during the object initialization, errors will not stop the process.
- public $sql;
- public function __construct(array $setValues=NULL) {
- xdebug_break();
- $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
- $this->dsn = $arrInitDefault["dsn"];
- $this->username = $arrInitDefault["username"];
- $this->password = $arrInitDefault["password"];
- $this->db = $arrInitDefault["db"];
- $this->arrConnAttr = implode(",", $arrInitDefault["arrConnAttr"]);
- $this->pathErroLog=$arrInitDefault["pathErroLog"];
- $this->die=$arrInitDefault["die"];
- xdebug_break();
- //This try and catch is only to estabilish the connection during the initialization
- try {
- $this->conn = new PDO("mysql:host=$this->dsn; dbname=$this->db", "$this->username", "$this->password");//Now private $conn is a PDO object
- $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Setting Attributes to the PDO object ($this->conn)
- }// End of try
- catch(PDOException $e){//If had some error. The PDO object ($this->conn) could not be created. Throw an error.
- $this->arrCatchConnResult = $this->saveLogError($e->getMessage());//Save a message in the log file and throw a message
- $msg= $this->arrCatchConnResult["strToHTML"];
- $this->conn = null;
- if($this->die){
- die($msg);
- }
- }
- }
- public function saveLogError($msg=NULL,$sql=NULL) {
- $year = date("Y");
- $month = date("M");
- $day = date("d");
- $dt = date("d.M.Y_H.i.s");
- $pathErroLog= $this->pathErroLog."/".$year."/".$month."/".$day."/";
- if (!is_dir($pathErroLog)) {
- mkdir($pathErroLog,0777,true);
- }//If is not a dir, create it.
- $sqlErrorStringTXT="";
- 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 function db_Map() {
- }
- public function get_values() {
- try {
- $stmt = $this->conn->prepare($this->sql);
- $stmt->execute();
- while ($field = $stmt->fetch(PDO::FETCH_ASSOC)) {
- $arrResult[]=$field;
- }
- return $arrResult;
- }
- catch (PDOException $e) {
- $this->arrCatchConnResult = $this->saveLogError($e->getMessage(), $this->sql);//Save a message in the log file and set a message
- $msg= $this->arrCatchConnResult["strToHTML"];
- $this->conn = null;
- if($this->die){
- die($msg);
- }
- }
- }
- }//End of class db
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement