How to Connect to MySQL Database using PHP

Hello dear readers! welcome back to another section of our tutorial on PHP. In this section of our PHP tutorial, we will be studying about MySQL Database Connection.

Opening Database Connection

PHP provides the mysql_connect function that opens a database connection. This function takes in five parameters and returns a MySQL link identifier on success or False on failure.


Following below is syntax of the PHP mysql_connect function -

connection mysql_connect(server,user,passwd,new_link,client_flag);

Parameter Details

Following below is the details of the above parameters -

Sr.NoParameter & Description


Optional − The host name running database server. If not specified then default value is localhost:3306.



Optional − The username accessing the database. If not specified then default is the name of the user that owns the server process.



Optional − The password of the user accessing the database. If not specified then default is an empty password.



Optional − If a second call is made to mysql_connect() with the same arguments, no new connection will be established; instead, the identifier of the already opened connection will be returned.



Optional − A combination of the following constants −

  • MYSQL_CLIENT_SSL − Use SSL encryption

  • MYSQL_CLIENT_COMPRESS − Use compression protocol

  • MYSQL_CLIENT_IGNORE_SPACE − Allow space after function names

  • MYSQL_CLIENT_INTERACTIVE − Allow interactive timeout seconds of inactivity before closing the connection


Note - You can specify a server, user, password in the php.ini file instead of using them again and again in every of your PHP script. Check out our tutorial on php.ini configuration.

Closing Database Connection

PHP provides the mysql_close function for closing of a database connection. This function takes the connection resource returned by mysql_connect function. When it is successful it returns True or False on failure.


Following below is the syntax of the mysql_close function -

bool mysql_close ( resource $link_identifier );

If a resource is not specified the last opened database is going to be closed.

Try the following example below to open and close a MySQL database connection -

   $dbhost = 'localhost:3036';
   $dbuser = 'guest';
   $dbpass = 'guest1234';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   if(! $conn ) {
      die('Could not connect: ' . mysql_error());
   echo 'Connected successfully';

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we will be discussing about how to create MYSQL database using PHP.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.