by Bhumi // Nov 16,2012 // 1 comment.
Originally I wrote a post about 1 year ago when i discovered some problems happened with me because of the MySQL connection fails. Now I have more experience with PHP/MySQL.I have decided to visit this topic to provide assured code for MySQL Connection.
|
1 2 3 |
$adminEmail = ""; $adminSMS = ""; |
You should use mysql_pconnect instead of mysql_connect because,mysql_pconnect() acts very much like mysql_connect() with two major differences.
First, when connecting, the function would first try to find a (persistent) link that’s already open with the same host, username and password. If one is found,an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect()).
This type of link is therefore called ‘persistent‘.
|
1 2 |
$link = @mysql_pconnect('localhost', 'username', 'password'); |
You should suppress the error message on failure by prepending a @ to the function name.
|
1 2 3 4 5 |
if (!$link) { $criticalError = "MySQL Server Connection Could not be Established"; CustomCriticalError($criticalError); } |
|
1 2 |
$db_selected = @mysql_select_db('database', $link); |
You should suppress the error message on failure by prepending a @ to the
function name.
|
1 2 3 4 5 |
if (!$db_selected) { $criticalError = "MySQL Database Could not be Connected"; CustomCriticalError($criticalError); } |
Above code is for if any error occur while connection is executing and here, I have called one function CustomCriticalError. Let’s see how function works!!
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
//Error Handling Custom Function function CustomCriticalError($errMsg){ //print "I am in Custom CriticalError function with $errMsg"; // Write Page Name, Time & Mysql Error -mysql_error() in a Log File $pagename = $_SERVER['PHP_SELF']; $daytime = date("l jS F Y h:i:s A"); $mysqlerror = mysql_error(); $errorLine = $pagename.",".$daytime.",".$mysqlerror; $logfilepath = realpath("."); $logfile = $logfilepath.'/mylog.txt'; $somecontent = $errorLine."\n"; // Let's make sure the file exists and is writable first. if (file_exists($logfile)) { //File Exist Here if (!$handle = fopen($logfile, 'a')) { //Log file could not be opened //Send Email & SMS $sendmail = @mail($adminEmail,"Critical Error & LogFile Open Failed" ,"Critical Error: $errorLine"); // Send SMS if Email was not able to sent if(!$sendmail){ // SMS Sending Code } // SMS Sending Code } } else{ //Log file does not exist $handle = @fopen($logfile, 'w'); } // Write $somecontent to our opened file. if (@fwrite($handle, $somecontent) === FALSE) { //Send Email & SMS //Could not write to log file; $sendmail = @mail($adminEmail,"Critical Error & LogFile Write Failed", "Critical Error: $errorLine"); // SMS Sending Code // Send SMS if Email was not able to sent if(!$sendmail){ } // SMS Sending Code } @fclose($handle); // Generate Email to Admin User $sendmail = @mail($adminEmail,"Critical Error","Critical Error: $errorLine"); if(!$sendmail){ // Send SMS if Email was not able to sent } // Generate instant SMS to Admin User // Show Some Friendly Message Page with Error Message Variable - $errMsg //print $errMsg; // We stop loading the page because there is no meanning in going further without // database connection or database exit; } |
Here, In above function CustomCriticalError, I have logged error in file and also to send E-mail and SMS if connection fails.I have explained all the codes in comment.
I hope you have enjoyed the concept of MySQL connection.Thanks for reading and feel free to share your thoughts, don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Chart is a graphical representation of dynamic data and php has very useful chart libraries using it easily create charts in php. So,Here is new READ MORE
To increase Product Imagesize in Opencart
Here,I come with one more new article in Opencart.In this quick article,I am going to share how to increare product image upload size into Opencart. READ MORE
Five Reasons Why You Should Go With PHP
Reasons simply incline us to believe in our decisions. They tend us to work hard to prove our decision as the productive ones. Therefore, you READ MORE
What Is Making People Insane for Cake PHP?
It is necessary to understand the increasing demands of cakePHP framework before going further to find credible programmers. Some other frameworks like Zend and Symphony READ MORE
Notice: Use of undefined constant HTTP_IMAGE in OpenCart
Here in this quick article, I am going to share solution of notice got in opencart.Opencart is the free,open source ecommerce system.OpenCart is robust and READ MORE
Be a Contributor at CreativeDev.Write an article or tutorial to the community and share some of your knowledge!
Pingback: Database BackUp Script | CreativeDev