PHP-MySQL Lesson – Connecting to MySQL database

Connecting to MySQL database

In order to access and manage records in a database, we need first to establish a connection to the database.

To connect to the MySQL database we are going to use the mysql_connect() function.

Here is the syntax:

mysql_connect (“$servername”,”$dbusername”,”$dbpassword”);

Parameters of mysql_connect() function:


The name of the computer or the ip address where the MySQL server resides, default value is localhost.


specifies the username to login or connect to MySQL, usually root.


specifies the password for the user account in the server.

Here is an example script:

$conn = mysql_connect("localhost","root","")
or die("could not connect");

In the above example we have used the $conn variable to handle or store the connection. Our servername is localhost, dbpassword is root and leave it blank in the password field. The die clause is the error handler of our script, it will display “could not connect” if the connection fails.

, ,

Post navigation

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.