How to Connect PHP with MySQL Database

<?php define(‘DB_SERVER’,’localhost’); define(‘DB_USER’,’root’); define(‘DB_PASS’ ,”); define(‘DB_NAME’, ‘dbname’); $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME); // Check connection if (mysqli_connect_errno()) {  echo “Failed to connect to MySQL: ” . mysqli_connect_error(); } ?>1. In the below  we can deifne our host name. For local you can use localhost. If you want to acces the other host then put that host  name or Server IP here

2.This code for db user name. For local we use root. But you can create  db user name whatever you want.
3. Here we can define db password. For local you should leave blank.
define('DB_PASS' ,'');
4.Here we define our  database name
define('DB_NAME', 'dbname');
5. The mysqli_connect() function opens a new connection to the MySQL server.
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.

Recommended Tutorials for you

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.