Sunday, October 8, 2017

php mysql connection

skptricks PHP connect to MySQL database example

Now a days, Database is required to stores, organizes and manages a large amount of information within a single software application. Records keeping is very easier for user, who is maintaining the large database. Database application manages data efficiently and allows users to perform multiple tasks with easily.

Today, we will discuss about MySQL database and also we will provide brief details, how we can connected to MySQL database with PHP application. In case you want to Learn PHP Basic, then do follow our blog and do post your comments. 

There are two ways to establish a connection with MySQL database using PHP :
  1. Using mysqli_connect() function.
  2. Using PDO::__construct() function.
NOTE : mysql_connect() extension is deprecated since PHP 5.5.
               mysqli_connect() function is not available form PHP 7

PHP mysqli_connect()

PHP mysqli_connect() function is used to connect with MySQL database. It returns resource if connection is established or null.

Syntax :
resource mysqli_connect (server, username, password)

PHP mysqli_close()

PHP mysqli_close() function is used to disconnect with MySQL database. It returns true if connection is closed or false.

bool mysqli_close(resource $resource_link)

PHP connect to MySQL database example

$host = 'localhost:3306';  // set the hostname
$user = '';   // Mysql DB username
$pass = '';  // Mysql DB password
//connecting to mysql database
$conn = mysqli_connect($host, $user, $pass);  
if(! $conn )  
echo 'Connected successfully to mysql database';  



  1. mysql_connect and other mysql_* functions aren't possible to use directly from PHP 7 on. Better to refactor this tutorial into PDO one instead.