Wed Jun 03 2020

How to connect PHP with MySQL Database

PHP Scripting3219 views


How to connect PHP with MySQL Database

File Name: mysql-connection.php

<?php
	/* Your mysql server host name */
	$host = "localhost";
	
	/* Mysql database user name */
	$user = "root";
	
	/* Mysql database user password */
	$password = "yourpassword";
	
	/* Mysql database name */
	$database = "test";
	
	
	/* Open connection mysql server in mysqli procedural */
	$connect = mysqli_connect($host, $user, $password, $database);
	
	if(!$connect)
        	die("Failed to connect with database!");

    	echo "Database connected!";
	
	/* Close Connection */
	mysqli_close($connect);
	


	/* Open connection mysql server in mysqli object oriented */
	$connect = new mysqli($host, $user, $password, $database);
	if($connect->error)
    	    die("Failed to connect with database");
    
    	echo "Database connected!";

	/* Close connection */
    	$connect->close();
?> 


/* Output */
Connection Establish with database!


Connection Establish with database!

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.