Connect MySQL using Procedural Way in PHP

Connecting MySQL database with PHP using Procedural way.(traditional and old method)

Source code :

$server = "localhost";
$username = "root";
$password = "";
//setting up the connection
$conn = mysqli_connect($server, $username, $password);
//checking the connection.
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
echo "Connected by procedural way successfully";

Output :

Connected by procedural way successfully

Notes :

To run the script:

1.Install any local host server and start it.
2.Type the above code in the installed directory

  • If it is WAMP,place the file in WWW folder (C:wamp64www).
  • If it is XAMPP,place the file in HTDocs folder (C:xampphtdocs).

and save it as php file.
3.In Browser,type localhost/filename.php and hit enter.

Note: Find an alternative and effective way to connect the database with php at

Output :


Comments :