MySQLi Tutorial MySQLi References

mysqli_connect() Function



The mysqli::__construct() / mysqli::connect() / mysqli_connect() function is used to open a new connection to the MySQL server.

Syntax

//Object-oriented style
public mysqli::__construct(hostname, username, password, 
                           database, port, socket)
public mysqli::connect(hostname, username, password, 
                       database, port, socket)

//Procedural style
mysqli_connect(hostname, username, password, 
               database, port, socket)

Parameters

hostname Optional. Specify the host name or an IP address. Passing the null value or "localhost" to this parameter, the local host is assumed.
username Optional. Specify the MySQL user name.
password Optional. Specify the MySQL password. This allows the username to be used with different permissions (depending on if a password is provided or not).
database Optional. If provided will specify the default database to be used when performing queries.
port Optional. Specify the port number to attempt to connect to the MySQL server.
socket Optional. Specify the socket or named pipe that should be used.

Return Value

Returns as follows:

  • mysqli::__construct() always returns an object which represents the connection to a MySQL Server, regardless of it being successful or not.
  • mysqli_connect() returns an object which represents the connection to a MySQL Server, or false on failure.
  • mysqli::connect() returns null on success or false on failure.

Exceptions

If MYSQLI_REPORT_STRICT is enabled and the attempt to connect to the requested database fails, a mysqli_sql_exception is thrown.

Example: Object-oriented style

The example below shows the usage of mysqli::__construct() method.

<?php
//enabling error reporting for mysqli 
//before attempting to make a connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");

printf("Success... %s\n", $mysqli->host_info);
?>

The output of the above code will be similar to:

Success... localhost via TCP/IP

Example: Procedural style

The example below shows the usage of mysqli_connect() function.

<?php
//enabling error reporting for mysqli 
//before attempting to make a connection
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");

printf("Success... %s\n", mysqli_get_host_info($mysqli));
?>

The output of the above code will be similar to:

Success... localhost via TCP/IP

Example: Object-oriented style - manual error handling

If error reporting is disabled, the user is responsible for checking and handling failures. Consider the example below:

<?php
error_reporting(0);
mysqli_report(MYSQLI_REPORT_OFF);

//establishing connection to the database
$mysqli = new mysqli("localhost", "user", "password", "database");

if ($mysqli->connect_errno) {
  throw new RuntimeException('mysqli connection error: ' . $mysqli->connect_error);
}

if ($mysqli->errno) {
  throw new RuntimeException('mysqli error: ' . $mysqli->error);
}
?>

Example: Procedural style - manual error handling

Consider the example below where error is handled manually in procedural style.

<?php
error_reporting(0);
mysqli_report(MYSQLI_REPORT_OFF);

//establishing connection to the database
$mysqli = mysqli_connect("localhost", "user", "password", "database");

if (mysqli_connect_errno()) {
  throw new RuntimeException('mysqli connection error: ' . mysqli_connect_error());
}

if (mysqli_errno($mysqli)) {
  throw new RuntimeException('mysqli error: ' . mysqli_error($mysqli));
}
?>

❮ MySQLi Functions Reference