Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > Database Related Code Examples

MySQL Connection

MySQL Connection <? // Base connection class for MySQL operations class mysql_conn { // Init variables required VAR $server = "" ; VAR $user = "" ; VAR $pwd = "" ; VAR $database = "" ; VAR $debuglv = true ; // Data related variables VAR $db = 0 ; VAR $error_level = 0 ; VAR $error_desc = "No errors" ; VAR $logfile = "datalog" ; VAR $filehdl = false ; VAR $msg = "" ; // Private methods function connect_to_db() { $this->db = mysql_connect ($this->server,$this->user,$this->pwd) ; $this->error_level = mysql_errno() ; $this->error_desc = mysql_error() ; if (!$this->db) { $this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - ERROR " . $this->error_level . ": " . $this->error_desc ; $this->debug() ; } else { mysql_select_db($this->database,$this->db) ; $this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION O.K.: Connected to database " . $this->database ; } } function release_db() { if ($this->db) { mysql_close($this->db) ; $this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION O.K.: Database " . $this->database . " released"; } else { $this->msg = "\r\n" . date("d/m/Y - H:i:s") . " - OPERATION FAILED: No database open"; } $this->debug() ; $this->logfile_close() ; } function logfile_init() { $fechagm = gmmktime()+3600 ; $fecha = getdate($fechagm) ; $this->logfile = $this->logfile . "-" . $fecha["mon"] . "-" . $fecha["year"] ; $this->filehdl = fopen($this->logfile,'a') ; if ($this->filehdl) { $this->msg = "\r\n" . date("d/m/Y - H:i:s") . " ===== SESSION STARTED BY " . $GLOBALS["PHP_SELF"] . " ====="; } else { echo "<!-- UNABLE TO OPEN SPECIFIED LOG FILE " . $this->logfile . " -->" ; } $this->debug() ; } function logfile_close() { if ($this->filehdl) { fclose($this->filehdl) ; } } function debug() { if ($this->debuglv) { if ($this->filehdl) { fwrite($this->filehdl,$this->msg) ; } else { echo "<!-- ".$this->msg."-->" ; } } } // Public interface function init() { $this->logfile_init() ; $this->connect_to_db() ; } function destroy() { $this->release_db() ; } function mysql_conn($servername="localhost",$username,$password,$databasename,$debuglevel=true) { $this->server = $servername ; $this->user = $username ; $this->pwd = $password ; $this->database = $databasename ; $this->debuglv = $debuglevel ; } } ?>