Back to Top

How to connect to MySQL database using PHP

MySQL Connection: Let be Expert

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 connect to MySQL database using PHP.

Let’s start to see one by one

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’.

You should suppress the error message on failure by prepending a @ to the function name.

You should suppress the error message on failure by prepending a @ to the function name.

Above code is for if any error occurs while the connection is executing and here, I have called one function CustomCriticalError. Let’s see how function works!!

Here, In above function CustomCriticalError, I have logged the error in the file and also to send E-mail and SMS if the connection fails.I have explained all the codes in a 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.

Share:
Share on Facebook0Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Comments (1)

  1. […] explaining such assured and flexible MySQL Connection which logs error in Email and SMS you want.I am going to explain how you can take backup of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Most Popular Posts

Cursor in IndexedDB API HTML5

Posted on 2 years ago

Bhumi

How to use CONST keyword in PHP

Posted on 5 years ago

Bhumi

How to set 301 Redirection in PHP

Posted on 2 years ago

Bhumi

Facade implementation in Laravel

Updated 3 weeks ago

Bhumi

MySQL coalesce() function

Updated 17 years ago

Bhumi

How to use JavaScript Promise API?

Updated 17 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 17 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi