We now have a youtube channel. Subscribe!

How to Connect to MySQL Database using PHP

PHP Database Connection

Hello folks! welcome back to a new section of our tutorial on PHP. In this section of our PHP tutorial, we are going to be discussing about MySQL Database Connection.

Opening Database Connection

PHP gives the mysql_connect function that opens a database connection. The function accepts five parameters and then returns a MySQL link identifier on success or False on failure.


Following below is syntax to use 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, password, user in the php.ini file instead of using them again and again in every of your PHP script. Read our tutorial on php.ini configuration.

Closing Database Connection

Closing Database Connection

PHP provides the mysql_close function for closing of a database connection. This PHP function takes the connection resource that was returned by mysql_connect function. If successful it returns TRUE otherwise FALSE on failure.


Following below is the syntax to use the mysql_close function -

bool mysql_close ( resource $link_identifier );

If a resource is not specified, then 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 are going to be discussing about how to create MYSQL database using PHP.

Feel free to ask your questions where necessary and we 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.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain