This is category for php code and script.
PHP Data Objects (PDO) is a powerful database access abstraction layer that provides a secure and efficient way to interact with databases. It supports a wide variety of database systems, making it a versatile choice for developers. In this guide, we’ll walk you through implementing PDO in your PHP scripts to enhance database operations. Whether you’re working on IT services, digital marketing platforms, or lead generation systems, PDO ensures secure and scalable database interactions.
PDO stands for PHP Data Objects. It is a lightweight and consistent interface for accessing databases in PHP. Unlike older methods such as `mysqli`, PDO provides support for multiple database types and comes with built-in features like prepared statements and error handling. Learn more about how PDO works on our Introduction to PDO page.
PDO offers several benefits that make it an essential tool for modern PHP development:
Before using PDO, you need to create a database. For this example, we’ll use MySQL to create a database and a table to store user information. For a detailed tutorial, visit our MySQL Database Setup Guide.
CREATE DATABASE pdo_demo;
USE pdo_demo;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Insert sample data into the `users` table:
INSERT INTO users (name, email) VALUES
('John Doe', 'john@example.com'),
('Jane Smith', 'jane@example.com');
This sample data will be used to test database operations with PDO.
To connect to your database using PDO, you need to create a PDO instance with the database credentials. Below is a simple example:
<?php
$host = 'localhost';
$dbname = 'pdo_demo';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully!";
} catch (PDOException $e) {
die("Connection failed: " . $e->getMessage());
}
?>
This script initializes a PDO instance and sets error handling to `ERRMODE_EXCEPTION`. Learn more about establishing PDO connections in our PDO Connections Tutorial.
Once connected, you can retrieve data from the database using the `query` method. Here’s an example of fetching all users from the `users` table:
<?php
$sql = "SELECT * FROM users";
$stmt = $pdo->query($sql);
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "Name: " . $row['name'] . ", Email: " . $row['email'] . "<br>";
}
?>
The `fetch()` method retrieves rows from the query, and `FETCH_ASSOC` returns them as an associative array. Learn more about fetching data with PDO on our Fetching Data with PDO page.
Implementing PDO in your PHP scripts improves the security, efficiency, and maintainability of your database operations. From connecting to a database and running queries to securing your application with prepared statements, PDO is an essential tool for modern web development. For more tutorials, visit our PHP Tutorials section and take your development skills to the next level!