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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/mysqli_connect.php