Introduction to Working with Databases in PHP
Modern web development is unthinkable without effective data management. PHP, as one of the most popular server-side languages, provides developers with powerful tools for interacting with various database management systems (DBMS). In this article, we will look at two fundamentally different but equally popular DBMS: relational PostgreSQL and document-oriented MongoDB.
For beginner developers, choosing between SQL and NoSQL can seem difficult. We will break down the key differences, learn how to connect to both databases from PHP, and perform basic CRUD operations (Create, Read, Update, Delete). By the end of the article, you will be able to independently choose the right DBMS for your project and write working code to interact with it.
1. PostgreSQL: Working with Relational Data via PDO
PostgreSQL is a powerful open-source object-relational DBMS. It supports complex queries, transactions, and advanced data types. To work with PostgreSQL in PHP, it is recommended to use the PDO (PHP Data Objects) extension, which provides a unified interface for working with different databases.
Connecting to PostgreSQL via PDO
The first step is to install the pdo_pgsql driver. Make sure it is enabled in your php.ini. After that, you can create a connection:
<?php$host = 'localhost';$dbname = 'my_database';$user = 'my_user';$password = 'my_password';
try { $pdo = new PDO("pgsql:host=$host;dbname=$dbname", $user, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connection to PostgreSQL established successfully!";} catch (PDOException $e) { die("Connection error: " . $e->getMessage());}?>CRUD Operations in PostgreSQL
Let's look at working with the users table, which contains the fields id, name, and email. All queries are executed using prepared statements to protect against SQL injection.
Create a record:
<?php$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";$stmt = $pdo->prepare($sql);$stmt->execute([':name' => 'Ivan Petrov', ':email' => 'ivan@example.com']);echo "User added. ID: " . $pdo->lastInsertId();?>Read data:
<?php$sql = "SELECT * FROM users WHERE id = :id";$stmt = $pdo->prepare($sql);$stmt->execute([':id' => 1]);$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user) { echo "Name: " . $user['name'] . ", Email: " . $user['email'];} else { echo "User not found.";}?>Update and Delete:
<?php// Update$sql = "UPDATE users SET email = :email WHERE id = :id";$stmt = $pdo->prepare($sql);$stmt->execute([':email' => 'new_email@example.com', ':id' => 1]);echo "Rows updated: " . $stmt->rowCount();
// Delete$sql = "DELETE FROM users WHERE id = :id";$stmt = $pdo->prepare($sql);$stmt->execute([':id' => 1]);echo "Rows deleted: " . $stmt->rowCount();?>2. MongoDB: Working with Documents via MongoDB PHP Library
MongoDB is a NoSQL database that stores data in flexible JSON-like documents. To work with it in PHP, the official mongodb/mongodb library is used, installed via Composer.
Installation and Connection to MongoDB
First, install the library via Composer:
composer require mongodb/mongodbAfter that, create a connection to the MongoDB server (by default it runs on port 27017):