PHP (Core & Framework)

How to connect MySQL database using PDO or Mysqli

We can use mysqli and PDO , to connect MySQL database in PHP. You can use any of the two, as both methods provide object oriented approach and prepared statement. The prepared sql statement is a must to prevent  SQL injection and web application security.  But the major difference between PDO & Mysqi is that you can connect 12 different databases using PDO, but with mysqli you can connect only MySQL database.

I think the above explanation gives you better idea which method you should select based on your project requirement.

You can use the code below to connect

mysql database.;

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;

try {
$conn = new PDO(“mysql:host=$servername;dbname=myDB”, $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();
}

You can find free php tutorials on w3schools.com

Comment here

− 1 = 2