Connecting MySQL using PHP


  • Access to a webserver (like Apache or IIS), PHP and MySQL. Installing XAMPP (pronounced "Shamp") can cater for all three applications in one go.

1. Make a file in PHP that will contain the parameters to connect to MySQL database.

2. In text editor, save a file "dbh.php" and copy paste the code below. Note: username, password, dbname values below to be replaced by actual values as per your database.


$dbServername = "localhost";
$dbUser = "root";
$dbpsswd = "";
$dbName = "login";

$conn = mysqli_connect($dbServername, $dbUser, $dbpsswd, $dbName);

3. Calling or referencing dbh.php in actual html / php webpages. Sample "index.php" file is given below.

 include_once 'include/dbh.php';

<!DOCTYPE html>
Connecting MySQL database using PHP