MySQLi Connection

mysqli_connect: Open a new connection to the MySQL server

Below the syntax

connection mysqli_connect(string $host, string $username, string $passwd, string $dbname, int $port, int $socket)


  • host: Can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.
  • username: The MySQL user name..
  • password: If not provided or NULL, the MySQL server will attempt to authenticate the user against those user records which have no password only. This allows one username to be used with different permissions (depending on if a password is provided or not).
  • dbname: If provided will specify the default database to be used when performing queries.
  • port: Specifies the port number to attempt to connect to the MySQL server.
  • socket: Specifies the socket or named pipe that should be used..

mysqli_close: Closes a previously opened database connection

bool mysqli_close(mysql $link)

Open the MySQL console and enter the password, you'll get the following.

mysqli connection

Example: The following examples to connect to a MySQL server.

   $conn  = mysqli_connect('localhost:3306', 'user_name', 'password');
   if(! $conn) {
       die('Could not connect: ' . mysqli_error());
   echo 'Connected successfully...' . mysqli_get_host_info($conn);

Output: Connected successfully...MySQL host info: localhost via TCP/IP