PHP | mysqli_connect() Function

PHP mysqli_connect() Function


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

The built-in PHP 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

If a connection got established successfully to the MYSQL server, this PHP function returns the connection object. Incase of an unsuccessful connection, this function returns the boolean value false.

PHP Version

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

Example1

The following below is an example which demonstrates 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 to create 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 will discuss 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.