PHP and PDO: How to handle binary data in databases
PHP and PDO: How to process binary data in the database
Introduction:
In the database, sometimes it is necessary to store and process binary data, such as pictures, audio and video files, etc. Using the PDO extension in PHP provides a simple and powerful way to handle binary data types in databases. This article will introduce how to use PDO to process binary data in the database and provide some code examples.
Connect to the database:
First, we need to connect to the database using PDO. The following is a simple code example:
$dsn = 'mysql:host=localhost;dbname=mydb'; $username = 'username'; $password = 'password'; try { $pdo = new PDO($dsn, $username, $password); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected to database successfully"; } catch (PDOException $e) { echo "Connection failed: " . $e->getMessage(); }
Reading binary data:
Next, we will learn how to read binary data from the database. Suppose we have a table named photos
, which has a column named photo_data
, which is used to store the binary data of the picture. The following code example demonstrates how to read the binary data of an image from the database:
$query = "SELECT photo_data FROM photos WHERE id = :id"; $stmt = $pdo->prepare($query); $stmt->bindParam(':id', $id); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_ASSOC); $photoData = $result['photo_data']; // 输出二进制数据 header('Content-Type: image/jpeg'); echo $photoData;
Write binary data:
Now, let's take a look at how to write binary data to the database. The following code example shows how to read image binary data from a file and save it to the photos
table:
$photoData = file_get_contents('path/to/photo.jpg'); $query = "INSERT INTO photos (photo_data) VALUES (?)"; $stmt = $pdo->prepare($query); $stmt->bindParam(1, $photoData, PDO::PARAM_LOB); $stmt->execute(); echo "Photo data inserted into database successfully";
Update binary data:
If you need to update an already stored For binary data, you can use the following code example:
$photoData = file_get_contents('path/to/new_photo.jpg'); $query = "UPDATE photos SET photo_data = ? WHERE id = ?"; $stmt = $pdo->prepare($query); $stmt->bindParam(1, $photoData, PDO::PARAM_LOB); $stmt->bindParam(2, $id); $stmt->execute(); echo "Photo data updated successfully";
Delete binary data:
When you no longer need the binary data in the database, you can use the following code example to delete it:
$query = "DELETE FROM photos WHERE id = :id"; $stmt = $pdo->prepare($query); $stmt->bindParam(':id', $id); $stmt->execute(); echo "Photo data deleted successfully";
Summary:
Using the PDO extension, we can easily handle binary data in the database. This article provides code examples of how to connect to a database, read, write, update, and delete binary data. By mastering these techniques, you can better handle binary data in your database and provide rich functionality to your applications.
The above is the detailed content of PHP and PDO: How to handle binary data in databases. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total
