PHP mysqli_connect() Function

PHP mysqli_connect() Function is used to open a new connection to the MySQL server. mysqli_connect() establishes a connection with MySQL server and returns the connection as an object.

PHP Version : PHP 5, PHP 7

Syntax for mysqli_connect:

According to Procedural

$link=mysqli_connect($SERVER,$USERNAME,$PASSWORD,$DATABASE,$PORT,$SOCKET) or die(mysqli_error()) ;

According to Object oriented



PHP mysqli_connect() Function
Parameter Description
$SERVER Required. It specifies server / host name.
$USERNAME Required. It specifies username of mysql.
$PASSWORD Required. It specifies password of mysql.
$DATABASE Optional. It specifies database name.
$PORT Optional. It specifies port number of mysql.
$SOCKET Optional. It specifies socket to be used.


Return values for mysqli_connect:

It returns an object representing the connection to the MySQL server. Otherwise it returns false, if connection is failed.


Example :

$connection = @mysqli_connect("localhost","username","password","database_name");
//check connection.
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL server : " . mysqli_connect_error();
echo "Mysql is connected successfully....";



Leave a Reply

Your email address will not be published. Required fields are marked *