PHP functions for MySql : mysqli_connect()

The mysqli_connect() function is used to open a connection to a MySQL Server. It returns an object representing the connection to the MySQL Server. The syntax of mysqli_connect() function is :
mysqli_connect(host,username,password,dbname,port,socket);
Where :
  • host specifies the host name or IP address of MySQL Server.
  • username specifies MySQLi user name.
  • password specifies the mysqli password.
  • dbname specifies mysqli database name you want to access
  • port specifies the port number to attempt to connect to the MySQL server.
  • socket specifies the socket name.
At above, the last three arguments dbname, port and socket are optional.

Example :
<?php

  $server = "localhost";
  $username = "username";
  $password = "";

  $connection = mysqli_connect($server, $username, $password);

  if(!$connection) {
    die("Connection failed: " . mysqli_connect_error());
  } else {
    echo "Successfully connected to MySQL database.";
  }

?>

Next Topic :