Làm cách nào để chèn vào BLOB MySQL?

Chèn hành động bằng cách sử dụng load_file để tải nội dung blob từ tệp được chỉ định

INSERT INTO table_name (file_name,file_content) VALUES("data.txt",LOAD_FILE("d:/data.txt"));
INSERT INTO table_name (file_name,file_content) VALUES("sample.txt",LOAD_FILE("d:/sample.txt"));

Bản tóm tắt. trong hướng dẫn này, bạn sẽ học cách xử lý dữ liệu BLOB bằng PHP PDO. Chúng tôi sẽ chỉ cho bạn cách chèn, cập nhật và chọn dữ liệu BLOB trong cơ sở dữ liệu MySQL

Làm cách nào để chèn vào BLOB MySQL?
Làm cách nào để chèn vào BLOB MySQL?

Đôi khi, vì lý do bảo mật, bạn có thể cần lưu trữ các đối tượng dữ liệu lớn, chẳng hạn như. g. , hình ảnh, tệp PDF và video trong cơ sở dữ liệu MySQL

MySQL cung cấp loại BLOB có thể chứa một lượng lớn dữ liệu. BLOB là viết tắt của đối tượng dữ liệu lớn nhị phân. Giá trị tối đa của đối tượng BLOB được chỉ định bởi bộ nhớ khả dụng và kích thước gói giao tiếp. Bạn có thể thay đổi kích thước gói giao tiếp bằng cách sử dụng biến 

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
0 trong MySQL và 

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
1 trong phần cài đặt PHP

Hãy xem cách PHP PDO xử lý loại BLOB trong MySQL

Đầu tiên, chúng ta tạo một bảng mới tên là

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
2 trong cơ sở dữ liệu mẫu để thực hành

Bảng

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
2 chứa ba cột

  • Cột id là khóa chính, cột tự động tăng
  • Cột mime lưu trữ loại mime của tệp
  • Cột dữ liệu có kiểu dữ liệu là BLOB được sử dụng để lưu trữ nội dung của tệp

Câu lệnh CREATE TABLE sau đây tạo bảng

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
2

CREATE TABLE files ( id INT AUTO_INCREMENT PRIMARY KEY, mime VARCHAR (255) NOT NULL, data BLOB NOT NULL );

Code language: SQL (Structured Query Language) (sql)

Thứ hai, chúng tôi định nghĩa một lớp có tên là

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
5 với đoạn mã sau

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)

Trong phương pháp

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
6, chúng tôi mở một kết nối cơ sở dữ liệu tới cơ sở dữ liệu MySQL và trong phương pháp 

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
7, chúng tôi đóng kết nối

Chèn dữ liệu BLOB vào cơ sở dữ liệu

PHP PDO cung cấp một cách thuận tiện để làm việc với dữ liệu BLOB bằng cách sử dụng các luồng và chuẩn bị các câu lệnh. Để chèn nội dung của tệp vào cột BLOB, bạn thực hiện theo các bước sau

  • Đầu tiên, mở tệp để đọc ở chế độ nhị phân
  • Thứ hai, xây dựng câu lệnh INSERT
  • Thứ ba, liên kết phần xử lý tệp với câu lệnh đã chuẩn bị bằng cách sử dụng phương thức  

    /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

    Code language: PHP (php)
    8 và gọi phương thức 

    /** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

    Code language: PHP (php)
    9 để thực hiện truy vấn

Xem phương pháp 

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
0 sau đây

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)

Lưu ý rằng 

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
1 hướng dẫn PDO ánh xạ dữ liệu dưới dạng luồng

Cập nhật cột BLOB hiện có

Để cập nhật cột BLOB, bạn sử dụng kỹ thuật tương tự như được mô tả trong việc chèn dữ liệu vào cột BLOB. Xem phương pháp 

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
2 sau đây

/** * update the files table with the new blob from the file specified * by the filepath * @param int $id * @param string $filePath * @param string $mime * @return bool */ function updateBlob($id, $filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "UPDATE files SET mime = :mime, data = :data WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); $stmt->bindParam(':id', $id); return $stmt->execute(); }

Code language: PHP (php)

Truy vấn dữ liệu từ cột BLOB

Các bước sau mô tả cách chọn dữ liệu từ cột BLOB

  • Đầu tiên, xây dựng một câu lệnh SELECT
  • Thứ hai, liên kết tham số tương ứng bằng phương pháp 

    /** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

    Code language: PHP (php)
    3 của đối tượng

    /** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

    Code language: PHP (php)
    4
  • Thứ ba, thực hiện tuyên bố

Xem phương pháp 

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
5 sau đây

/** * select data from the the files * @param int $id * @return array contains mime type and BLOB data */ public function selectBlob($id) { $sql = "SELECT mime, data FROM files WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->execute(array(":id" => $id)); $stmt->bindColumn(1, $mime); $stmt->bindColumn(2, $data, PDO::PARAM_LOB); $stmt->fetch(PDO::FETCH_BOUND); return array("mime" => $mime, "data" => $data); }

Code language: PHP (php)

Các ví dụ PHP MySQL BLOB

Trong các ví dụ sau, chúng ta sẽ sử dụng lớp

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
5 để lưu ảnh GIF và tệp PDF vào cột BLOB của bảng

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
2

PHP MySQL BLOB với các tệp hình ảnh

Đầu tiên, chúng tôi chèn dữ liệu nhị phân từ tệp

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
8 vào cột BLOB của bảng

/** * PHP MySQL BLOB Demo */ class BlobDemo { const DB_HOST = 'localhost'; const DB_NAME = 'classicmodels'; const DB_USER = 'root'; const DB_PASSWORD = ''; /** * Open the database connection */ public function __construct() { // open database connection $conStr = sprintf("mysql:host=%s;dbname=%s;charset=utf8", self::DB_HOST, self::DB_NAME); try { $this->pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD); //for prior PHP 5.3.6 //$conn->exec("set names utf8"); } catch (PDOException $e) { echo $e->getMessage(); } } /** * close the database connection */ public function __destruct() { // close the database connection $this->pdo = null; } }

Code language: PHP (php)
2 như sau

$blobObj = new BlobDemo(); // test insert gif image $blobObj->insertBlob('images/php-mysql-blob.gif',"image/gif");

Code language: PHP (php)
Làm cách nào để chèn vào BLOB MySQL?
Làm cách nào để chèn vào BLOB MySQL?

Sau đó, chúng ta có thể chọn dữ liệu BLOB và hiển thị dưới dạng ảnh GIF

$a = $blobObj->selectBlob(1); header("Content-Type:" . $a['mime']); echo $a['data'];

Code language: PHP (php)
Làm cách nào để chèn vào BLOB MySQL?
Làm cách nào để chèn vào BLOB MySQL?

PHP MySQL BLOB với tệp PDF

Đoạn mã sau chèn nội dung của  tệp PDF 

/** * update the files table with the new blob from the file specified * by the filepath * @param int $id * @param string $filePath * @param string $mime * @return bool */ function updateBlob($id, $filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "UPDATE files SET mime = :mime, data = :data WHERE id = :id;"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); $stmt->bindParam(':id', $id); return $stmt->execute(); }

Code language: PHP (php)
0 vào cột BLOB

$blobObj = new BlobDemo(); // test insert pdf $blobObj->insertBlob('pdf/php-mysql-blob.pdf',"application/pdf");

Code language: PHP (php)
Làm cách nào để chèn vào BLOB MySQL?
Làm cách nào để chèn vào BLOB MySQL?

Sau đó, chúng ta có thể chọn dữ liệu PDF và hiển thị nó trong trình duyệt web như sau.

$a = $blobObj->selectBlob(2); header("Content-Type:" . $a['mime']); echo $a['data'];

Code language: PHP (php)
Làm cách nào để chèn vào BLOB MySQL?
Làm cách nào để chèn vào BLOB MySQL?

Để thay thế tệp PDF bằng tệp ảnh GIF, bạn sử dụng phương pháp 

/** * insert blob into the files table * @param string $filePath * @param string $mime mimetype * @return bool */ public function insertBlob($filePath, $mime) { $blob = fopen($filePath, 'rb'); $sql = "INSERT INTO files(mime,data) VALUES(:mime,:data)"; $stmt = $this->pdo->prepare($sql); $stmt->bindParam(':mime', $mime); $stmt->bindParam(':data', $blob, PDO::PARAM_LOB); return $stmt->execute(); }

Code language: PHP (php)
2 như sau.

$blobObj->updateBlob(2, 'images/php-mysql-blob.gif', "image/gif"); $a = $blobObj->selectBlob(2); header("Content-Type:" . $a['mime']); echo $a['data'];

Code language: PHP (php)

Bạn có thể tải xuống mã nguồn của hướng dẫn này qua liên kết sau

Tải xuống mã nguồn PHP MySQL BLOB

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách quản lý dữ liệu MySQL BLOB, bao gồm chèn, cập nhật và truy vấn blob

Làm cách nào để chỉnh sửa dữ liệu blob trong MySQL?

BLOB Editor được gọi từ lưới dữ liệu của bất kỳ trình soạn thảo bảng nào hoặc tab kết quả của SQL Editor và Visual Query Builder bằng cách nhấp đúp vào trường BLOB cần chỉnh sửa hoặc bằng nút Chỉnh sửa . Trình chỉnh sửa cũng có thể được gọi từ Trình xem BLOB bằng nút Chỉnh sửa BLOB hiện tại. . The editor also can be called from BLOB Viewer with the Edit current BLOB button.

Chúng ta có thể chèn BLOB trong SQL không?

Việc sử dụng các ký tự nhị phân SQL để chèn các giá trị BLOB vào cơ sở dữ liệu rất đơn giản, nhưng nó yêu cầu bạn chuyển đổi dữ liệu nhị phân của mình sang định dạng ký tự nhị phân SQL. ' , đây có thể là vấn đề nếu bạn nhập dữ liệu nhị phân rất dài. Lưu ý rằng định dạng chữ nhị phân trên Oracle khác với MySQL.

Làm cách nào để sử dụng kiểu dữ liệu BLOB trong MySQL?

BLOB là một đối tượng lớn nhị phân có thể chứa một lượng dữ liệu thay đổi . Bốn loại BLOB là TINYBLOB , BLOB , MEDIUMBLOB và LONGBLOB. Chúng chỉ khác nhau về độ dài tối đa của các giá trị mà chúng có thể giữ. Bốn loại TEXT là TINYTEXT , TEXT , MEDIUMTEXT và LONGTEXT.

Làm cách nào để lấy dữ liệu từ BLOB trong MySQL?

Thực hiện theo các bước sau. Thay đổi bảng và thêm cột có kiểu dữ liệu TEXT. Thêm nội dung vào cột đó sau khi chuyển đổi dữ liệu BLOB sang ngày TEXT