PHP | mysqli_connect() Function

PHP mysqli_connect() Function


Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP mysqli_connect() Function.

The mysqli_connect() function establishes a connection with MYSQL server and returns the connection as an object.

Syntax

Following below is the syntax to use this function -

mysqli_connect([$host, $username, $passwd, $dname, $port, $socket] )


Parameter Details

Sr.NoParameter & Description
1

host(Optional)

This represents a host name or an IP address. If you pass Null or localhost as a value to this parameter, the local host is considered as host.

2

username(Optional)

This represents a user name in MySQL.

3

passwd(Optional)

This is represents the password to the given user.

4

dname(Optional)

This represents the default database in which the queries should be performed.

5

port(Optional)

This represents the port number at which you want to establish a connection to MySQL Server.

6

socket(Optional)

This represents the socket that is to be used.


Return Value

This built-in function returns the connection object, if a connection got established to a MYSQL server successfully. In case of an unsuccessful connection, this built-in PHP function returns the boolean value false.

PHP Version

This built-in function was first introduced in PHP version 5 and it works in all of the later versions.

Example1

The following example below illustrates the usage of PHP mysqli_connect() function (in a procedural style) -

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = mysqli_connect($host, $username, $passwd, $dbname);

   if($con){
      print("Connection Established Successfully");
   }else{
      print("Connection Failed ");
   }
?>

Output

When the above code is executed, it will produce the following result -

Connection Established Successfully

Example2

In an object oriented style you can use new mysqli() construct in creating a connection as follows $minus;

<?php
   $host = "localhost";
   $username  = "root";
   $passwd = "password";
   $dbname = "mydb";

   //Creating a connection
   $con = new mysqli($host, $username, $passwd, $dbname);

   if($con->connect_errno){
      print("Connection Failed ");
   }else{
      print("Connection Established Successfully");
   }

   //Closing the connection
   $con -> close();
?>

Output

When the above code is executed, it will produce the following result -

Connection Established Successfully

Example3

You can also invoke this function without passing any parameters as shown below -

<?php
   //Creating a connection
   $con = @mysqli_connect();

   if($con){
      print("Connection Established Successfully");
   }else{
      print("Connection Failed ");
   }
?>

Output

When the above code is executed, it will produce the following result -

Connection Failed

Example4

Try the following example below -

<?php
   $connection_mysql = @mysqli_connect("localhost", "root", "wrong_password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
	  exit();
   }   
   echo ("Connection established successfully");    
   mysqli_close($connection_mysql);
?>

Output

When the above code is executed, it will produce the following result -

Failed to connect to MySQL: Access denied for user 'root'@'localhost' (using password: YES)


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be studying about the PHP mysqli_connect_errno() Function.

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

Do follow us on our various social media handles available and also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.