w3resource

PHP mysqli: connect() function

mysqli_connect() function / mysqli::__construct

The mysqli_connect() function / mysqli::__construct opens a new connection to the MySQL server.

Syntax:

Object oriented style

mysqli::__construct ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

Procedural style

mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )

Usage: Procedural style

mysqli_connect(host,username,password,dbname,port,socket);

Parameter:

Name Description Required/Optional
host Specifies a host name or an IP address Optional
username Specifies the MySQL username Optional
password Specifies the MySQL password Optional
dbname Specifies the default database to be used Optional
port Specifies the port number to attempt to connect to the MySQL server Optional
socket Specifies the socket or named pipe to be used Optional

Return value:

Returns an object which represents the connection to a MySQL Server.

Version: PHP 5, PHP 7

Example of object oriented style:

<?php
mysqli::__construct ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
?>

Example of procedural style:

<?php
mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )
?>

See also

PHP Function Reference

Previous: connect_error
Next: debug



Follow us on Facebook and Twitter for latest update.