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 Posts
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

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top