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.

Syntax

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
1

server

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

2

user

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

3

passwd

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

4

new_link

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.

5

client_flags

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.

Syntax

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.

Example

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

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


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.