Posted on Arjun

How to Connect to mysql database with php?

before any operations are to be made on the data, you have to establish a connection to the DB. There is Two types of connection are exist , normal connection and persistent connection

persistent connection doesn’t differ from usual connection, except that different function is used for connection establishment, if you close the persistent connection it will not effect.

There is no much difference in b/w the two connection types , in normal single-time MySQL connections are created, and closed once script execution is completed.This is good for rare connections, when PHP page isn’t requested too frequently.

In persistent connections type it will keep connection open for some particular time, and if the page loads multiple times, PHP code will reclaim the same connection.

Establishing connection

we Use the PHP mysql_connect() function to open a new connection to the MySQL Database.
we use the PHP mysql_pconnect() function to open a new persiste connection to the MySQl Database.

Both the function has same parameter , both used exactly in same way.

Selecting mysql database

here $conn is an link identifier, it sets that current active database is associated with the specified link identifier . if you not set that link identifier it will associate with last link opened by mysql_connect().if it didn’t find any connection it return error message

Complete Example :: MySQL database connection with PHP

Complete Example :: MySQL database connection with PHP in OPPs

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status