In PHP you can easily do this using the mysqli_connect() function. All communication between PHP and MySQL database server takes place through this connection.

Syntax : MySQLi Procedural

$link = mysqli_connect("hostname", "username", "password", "database");

Example : MySQLi Procedural

<?php

    // Create connection
    $link = mysqli_connect("localhost", "root", "password");

    // Check connection
    if (!$link) {
      die("Connection failed: " . mysqli_connect_error());
    }
    echo "Connected successfully";
?>

Syntax : MySQLi PDO

$pdo = new PDO("mysql:host=hostname;dbname=database", "username", "password");

Example : MySQLi PDO

<?php

    try {
      $link = new PDO("mysql:host=hostname;dbname=database", "root", "password");
      // set the PDO error mode to exception
      $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      echo "Connected successfully";
    } catch(PDOException $e) {
      echo "Connection failed: " . $e->getMessage();
    }
?>

Syntax : MySQLi Object Oriented

$link = new mysqli("hostname", "username", "password", "database");

Example : MySQLi Object Oriented

<?php

    // Create connection
    $link = new mysqli("localhost", "root", "password", "database");

    // Check connection
    if ($link->connect_error) {
      die("Connection failed: " . $link->connect_error);
    }
    echo "Connected successfully";
?>