PHP MySQL Database Connection

In this tutorial we will discuss how to create a database connection using PHP 7.1.9. and MySQL 5.7. The latest versions of PHP 7.4.5 released on 16 April 2020. We use the WAMPServer 3.1.0 server to manage databases.

How to create a database in MySQL wamp server?

Start run installed WAMPServer in your machine. Then click the WAMPServer icon on the taskbar right side. You can see this below menu in after click wamp icon.

Select the phpMyAdmin on WAMP server menu
WAMP server menu

Next, select the phpMyAdmin or open your web browser and type http://localhost/phpmyadmin/ on browser search bar.

Enter username (default-root) and your database password (default-empty). Then select server as MySQL and click ‘Go‘.

WAMP server login form

The admin page window will open. After the page opens in your left side window, click new. Enter the database name (MyDB) like as following.

Create database in WAMP server

Your database will present on the left side phpMyAdmin page.

Create a PHP database connection with MySQL

Open your text editor and copy one php code to your text editor. Enter username as your database username (default username is root). If your database has a password, enter this correct password under a $password. Then use the select DB name as a need to connect the database.

a. Database Connect to MySQL using MySQLi


$localhost = ""; //use this ip or 'localhost'
$username  = "root"; //database username
$password  = "";     //database password
$dbname    = "mydb"; //database name

//database connection
$connect = new mysqli($localhost, $username, $password, $dbname);
// check connection
if($connect->connect_error) {
  die("Connection Failed : " . $connect->connect_error);
} else {
  // echo "Database Successfully Connected";


b. Database Connect to MySQL using PDO


define('DB_SERVER', 'localhost');//server name
define('DB_USERNAME', 'root'); //username
define('DB_PASSWORD', '');     //database password
define('DB_NAME', 'mydb');     //database name
/*connect to MySQL database */

try {
    $pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
    die("Database Successfully Connected");
catch(PDOException $e) {
    die("ERROR: Could not connect. " . $e->getMessage());


Click WAMPServer icon and select the www directory in WAMPServer menu.

Select www directory

Then create a new folder(ex: website) in the open www directory. Save database connection PHP code in a created folder. You must use .php extension for save PHP code (ex: dbconnection.php).

Select the WAMPServer menu Localhost or type http://localhost/ on your web browser.

Then type http://localhost/website/dbconnection.php on your browser search bar. When correctly connected database connection can see below on your browser.

success connection to MySQL Database in PHP.

PHP is a popular open-source general-purpose scripting language. You can learn PHP programming with examples by following our PHP articles sequentially.

 750 total views,  1 views today

Leave a Comment

Your email address will not be published. Required fields are marked *