PHP mysqli_connect() Function

Definition

The mysqli_connect() function opens a new connection to the MySQL server.

Syntax

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

Parameter

ParameterIs RequiredDescription
hostOptional.Host name or an IP address
usernameOptional.MySQL username
passwordOptional.MySQL password
dbnameOptional.Default database to be used
portOptional.Port number to attempt to connect to the MySQL server
socketOptional.Socket or named pipe to be used

Return

It returns an object representing the connection to the MySQL server.

Example

The following code opens a new connection to the MySQL server.


<?php//  w  w w.j a  v  a  2  s.c  o  m
$con=mysqli_connect("localhost","my_user","my_password","my_db");

// Check connection
if (mysqli_connect_errno($con)){
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions