How to Connect PHP with MySQL Database
connect PHP with mysql database in your project
Code for MySQL connection with 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();
}
Step -1: In the below we can define our hostname. For local, you can use localhost. If you want to access the other host then put that hostname or Server IP here:
define('DB_SERVER','localhost');
Step -2: This code for db user name. For local we use root. But you can create db user name whatever you want.
define('DB_USER','root');
Step -3: Here we can define db password. For local you should leave blank.
define('DB_PASS','root');
Step -4: Here we define our database name.
define('DB_NAME', 'dbname');
Step -5: The mysqli_connect() function opens a new connection to the MySQL server.
$con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);