Introduction to PHP | MySQL Database

Infraveo Technologies
3 min readSep 20, 2022

--

MySQL: Open-Source Relational Database Management System

Introduction

In this article, we will learn about MySQL database in detail. We will explain what a database is, what is MySQL, and its advantages over other databases including Oracle, SQL Server, and other popular databases.

What is Database?

A database is a collection of information. It contains data such as customers, orders, and products in a table. Each row in the table has fields like customer ID, phone number, and address.

A database consists of tables that have columns for storing data. These tables are related to each other by using foreign keys (or one-to-many relationship) or primary keys (or one-to-one relationship).

A database may also contain some special types of files called indexes which help in fast searching through big volumes of information stored in databases by only looking up few bits at a time instead of scanning through all records sequentially which would take too long time

What is MySQL?

MySQL is a relational database management system (RDBMS). It is an open-source product, developed by Oracle Corporation.

MySQL is a server-side database system, and it runs on many operating systems like Linux, Windows, and Mac OS X.

It supports SQL as its query language which means you can use SQL to write queries against your data tables in MySQL database.

PHP connection to MySQL database

There are three ways to connect to MySQL using PHP, which are given above and explained below:

Using MySQLi object-oriented procedure:

From a PHP script, we can connect to the MySQL database using the MySQLi object-oriented method.

Syntax-

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

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

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

Interpretation: By giving the host, username, password, and other information essential to make the connection, we may construct an instance of the mysqli class. If the instance is successfully generated, the connection will also be successful; otherwise, there will be a connection error.

Using the procedural technique of MySQLi:

As seen below, MySQLi also offers a procedural method for connecting to a MySQL database from a PHP script.

Syntax-

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

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

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

Interpretation: In the MySQLi procedural approach, we can create a connection using the PHP function mysqli connect() rather than by creating an instance. Information like the host, username, password, database name, etc. are passed as arguments to this function. In the event that a connection is established successfully, this method returns the MySQL link identifier; otherwise, it returns FALSE.

PDO stands for PHP Data Objects.

PDO process. To connect to the database using data objects in PHP, use the following technique:

Syntax-

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

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

Interpretation: PDO’s exception classes are used to handle problems that may occur in database queries. If an exception is thrown inside a try{ } block, script execution stops and goes directly to the first catch(){ } block.

Conclusion

In the end, it is up to you whether or not you want to use MySQL or not. However, we would recommend trying out both systems as they each have their own benefits and disadvantages. For more information on this subject, read our blog on MySQL.

--

--

Infraveo Technologies

We are a proud team of IT engineers who thrive upon rolling up their sleeves to solve your IT problems.