Learn how to connect mysql database using php – PHP tutorial

Learn how to connect mysql database using php – A simple and short PHP tutorial. Before going to run any query on database you need to connect with it. So for any operation on database like insert, update, delete first you have to create a connection with the database means Open a connection to a database with PHP otherwise this mysql query not working with this database. So in this post learn how to connect mysql database using php. I think It will be very helpful for new comers in php programming.

For any web application with php script and database driven there need to configure or create a connection with a databse, only then you can run mysql query on the database and get the expected output. Here i described basically 3 types of mysql connection which is mostly used in any php web application to connect with database.

Mysql connection.(Used earlier)
Mysqli connection.(Improve version of mysql. ‘l’ stands for improve)
PDO connection.(PHP data object)

Basic difference between mysql/mysqli and PDO connection:
PDO will work on 12 different database systems, where as MySQLi will only work with MySQL databases.So, if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code – queries included.

Learn how to connect mysql database using php.

Mysql connection

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database= "database";

// Create connection
$conn = mysql_connect($servername, $username, $password);

// Check connection working or not
if (!$conn) {
    die("Connection failed: " . mysql_connect_error());
}
echo "Connected successfully";
?>

Mysqli connection

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database= "database";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection working or not
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
//select db for which the connection will work
mysqli_select_db($database, $conn) or die("Error on database connection");
?>

PDO connection

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
    $conn = new PDO("mysql:host=$servername;dbname= database", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully"; 
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
?>

After your query execution you need to close the connection.
Close the connection:

mysql_close($conn); // for mysql connection
mysqli_close($conn); // for mysqli connection 
$conn = null; // for PDO connection

In all the php frameworks and php web application there should be a separate connection file. For better understanding we named that file dbconnect.php or dbconfig.php or config.php. And where require to perform any query to moderate database before that we just include the database connection file. Please try yourself in localhost or in any server. For any further clarification in this topic feel free to contact. If you think this post will help beginners then please share it. Also you can go through our earlier post php login script tutorial, php login tutorial with encryption, form submit without page refresh using ajax. Subscribe or follow to get updated with our new post.

2 responses to “Learn how to connect mysql database using php – PHP tutorial”

  1. Steven says:

    Very interesting content, your blog can go viral.

Leave a Reply

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