WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP Connect to MySQL


PHP 5 and later can work with a MySQL database using:

  • MySQLi extension (the "i" represents improved)
  • PDO (PHP Data Objects)

Earlier variants of PHP utilized the MySQL augmentation. In any case, this augmentation was belittled in 2012.


Should I Use MySQLi or PDO?

If you need a short answer, it would be "Whatever you like".

Both MySQLi and PDO have their advantages:

PDO will chip away at 12 distinctive database frameworks, where as MySQLi will as it were work with MySQL databases.

So, on the off chance that you need to switch your task to utilize another database, PDO makes the procedure simple. You just have to change the association string and a couple of inquiries. With MySQLi, you will require to rework the whole code - inquiries included.

Both are object-situated, yet MySQLi likewise offers a procedural API.

Both bolster Prepared Statements. Arranged Statements ensure from SQL infusion, and are imperative for web application security.


MySQL Examples in Both MySQLi and PDO Syntax

In this, and in the accompanying parts we show three different ways of working with PHP and MySQL:

  • MySQLi (object-oriented)
  • MySQLi (procedural)
  • PDO

MySQLi Installation

For Linux and Windows: The MySQLi augmentation is naturally introduced in most cases, when php5 mysql bundle is installed.

For establishment subtleties, go to: http://php.net/manual/en/mysqli.installation.php


PDO Installation

For establishment subtleties, go to: http://php.net/manual/en/pdo.installation.php


Open a Connection to MySQL

Before we can get to information in the MySQL database, we should probably interface with the server:

Example (MySQLi Object-Oriented)

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

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

//Check connection
on the off chance that ($conn->connect_error) {
    die("Connection fizzled: " . $conn->connect_error);
}
echo "Connected successfully";
?>

Example (MySQLi Procedural)

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

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

//Check connection
in the event that (!$conn) {
    die("Connection fizzled: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

Example (PDO)

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

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

Tip: An incredible advantage of PDO is that it has a special case class to deal with any issues that may happen in our database inquiries. On the off chance that a special case is tossed inside the try{ } square, the content quits executing and streams specifically to the first catch(){ } block.


Close the Connection

The association will be shut consequently when the content closures. To close the association previously, utilize the following:

Example (MySQLi Object-Oriented)

$conn->close();

Example (MySQLi Procedural)

mysqli_close($conn);

Example (PDO)

$conn = invalid;