Connecting MySQL using PHP

Pre-Requisites:

  • 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.

<?php

$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.

<?php
 include_once 'include/dbh.php';

?>
<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
Connecting MySQL database using PHP
$conn

</body>
</html>