How to make connect mysql database with PHP

PHP MYSQLI

How to make connection MySQL database for access database.

mysql_connect helps to make connection to MySQL Server


Connect with MySQLi Procedural

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

$conn = mysqli_connect($servername, $username, $password,$database);

if (!$conn) {
    die(“Connection failed: ” . mysqli_connect_error());
}
echo “Connected successfully”;
?>

Connect with  MySQLi Object-Oriented :

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

$conn = new mysqli($servername, $username, $password);

if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}
echo “Connected successfully working”;
?>

Connect with  PDO (PHP Data Objects) :

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
try {
    $conn = new PDO(“mysql:host=$servername;dbname=myDB”, $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo “Connected successfully”;
    }
catch(PDOException $e)
    {
    echo “Connection failed: ” . $e->getMessage();
    }
?>

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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