How to Connect to phpmyadmin Database in PHP

Connect phpmyadmin MySQL database in PHP with xampp; In this tutorial, i would like to show you how to connect phpmyadmin database in php with xampp code.

How to Connect to PHPMyAdmin Database in PHP

Let’s use the below given php code to connect phpmyadmin database with xampp.

  • First of all, start xampp server. Then start apahce and mysql server.
  • Then open your browser and type localhost/phpmyadmin
  • After that, create new database and table
  • Then navigate to xampp/htdocs and create heree one file named mydb.php.
  • And open mydb.php file on any text editor.
  • Then add the below given code into mydb.php file.
  • Now open your browser and type localhost/mydb.php.

Let’s, add the following code in your mydb.php file for connecting a phpmyadmin database in php with xampp:

<?php

$hostName = "localhost";
$userName = "username";
$password = "password";

// Create connection
$conn = mysqli_connect($host, $username, $password);

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

mysqli_close($conn);
?>

Explaination of php program to connect mysql database:

The server name, database, username, and password and their respective values should correspond to your phpmyadmin connection.

The PHP function mysqli_connect(). It open a connection with the specified phpmyadmin database.

The “if statement.” is used to check whether the connection was established or not. If the phpmyadmin connection fails, it return the message Connection failed.

If the connection is successful, it displays “Connected successfully.”

At the last of php script, the phpmyadmin database connection also closes. If you want to close phpmyadmin database manually, use the mysqli_close function.

Conclusion

PHPmyadmin database connection in php with mysql in xampp code. Here you have learned how to connect phpmyadmin database in php with xampp code.

Recommended PHP Tutorials

Leave a Comment