Monday, October 5, 2020

How to Connect to MySQL Database using PHP



Hello dear readers! welcome back to another section of my tutorial on PHP. In this section of my 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.

Syntax

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
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


RECOMMENDED: PHP MySQL Database

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.

Syntax

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.

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 my 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.
Share:

0 comments:

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.