PHP Connect to MySQL

If you\’re using PHP 5 or later, you can use:

  • MySQLi extension (the \”i\” stands for improved)
  • PDO (PHP Data Objects) (PHP Data Objects)

When PHP was first made, it used the MySQL extension. But this extension stopped being used in 2012.

Should I use PDO or MySQLi?

If you want a short answer, \”Whatever you like\” would be the right answer.

MySQLi and PDO both have their interesting facts:

MySQLi will only work with MySQL databases, but PDO will work with 12 different database systems.

So, if you need to change your project so that it uses a different database, PDO makes it easy to do so. Only the connection string and a few queries need to be changed. With MySQLi, you will have to write the code all over again, including the queries.

Both MySQL and MySQLi are based on objects, but MySQLi also has a procedural API.

Both of them agree with Prepared Statements. SQL injection can be stopped with Prepared Statements, which are a very important part of web application security.

There are examples of MySQL in both MySQLi and PDO syntax.

In this chapter and the ones that follow, we show you three ways to work with PHP and MySQL:

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

MySQLi Installation

Most of the time, when the php5 mysql package is installed, the MySQLi extension is installed automatically on Linux and Windows.

Go to http://php.net/manual/en/mysqli.installation.php for information on how to set up.

Installation of PDO

Go to http://php.net/manual/en/pdo.installation.php for information on how to set up.

Start a Connection with MySQL

Before we can use the MySQL database, we need to be able to connect to the server:

Example (MySQLi Object-Oriented)

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

// connection check
$link = new mysqli($servername, $username, $password);

// connection check
if ($link->connect_error) {
die(\”Connection failed: \” . $link->connect_error);
}
echo \”Connected successfully\”;
?>

Example (MySQLi Procedural)

<?php
$servername = \”localhost\”;
$username = \”username\”;
$password = \”password\”;
// connection create
$link = mysqli_connect($servername, $username, $password);

// connection check
if (!$link) {
die(\”Connection failed: \” . mysqli_connect_error());
}
echo \”Connected successfully\”;
?>

PDO Example

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

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

One of the best things about PDO is that it has an exception class to deal with any problems that might come up when we query our database. If an error occurs in the try block, the script stops running and goes straight to the first catch block.

Close the Connection

When the script is done, the connection will be closed on its own. Use the following to end the connection:

Object-oriented MySQLi:

 

$conn->close();

Procedures for MySQLi:

 

mysqli close($conn);

PDO:

 

$conn = null;

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more