Mysql chèn ảnh INTO BLOB

Tóm lược. 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

Mysql chèn ảnh INTO BLOB
Mysql chèn ảnh INTO BLOB

Đô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 liên lạc 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 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)
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 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)
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)
Mysql chèn ảnh INTO BLOB
Mysql chèn ảnh INTO BLOB

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)
Mysql chèn ảnh INTO BLOB
Mysql chèn ảnh INTO BLOB

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)
Mysql chèn ảnh INTO BLOB
Mysql chèn ảnh INTO BLOB

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)
Mysql chèn ảnh INTO BLOB
Mysql chèn ảnh INTO BLOB

Để 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èn hình ảnh trong BLOB MySQL?

Chèn Hình ảnh và Tệp dưới dạng dữ liệu BLOB vào Bảng MySQL .
Cài đặt MySQL Connector Python bằng Pip
Thứ hai, Thiết lập kết nối cơ sở dữ liệu MySQL trong Python
Tạo một chức năng có thể chuyển đổi hình ảnh và tệp thành dữ liệu nhị phân
Sau đó, Xác định truy vấn Chèn để nhập dữ liệu nhị phân vào bảng cơ sở dữ liệu

Chúng tôi có thể lưu trữ hình ảnh dưới dạng BLOB không?

Đối tượng lớn nhị phân ( BLOB ) là loại dữ liệu MySQL có thể lưu trữ dữ liệu nhị phân như hình ảnh, đa phương tiện và tệp PDF .

Làm cách nào để chèn hình ảnh vào MySQL?

1 câu trả lời. Bạn có thể thử đoạn mã dưới đây để chèn hình ảnh. CHÈN VÀO GIÁ TRỊ xx_BLOB(ID,IMAGE)(1,LOAD_FILE('E. /Hình ảnh/giắc cắm. jpg'));

Làm cách nào để chèn hình ảnh BLOB vào MySQL bằng PHP?

Khi gửi biểu mẫu, mã PHP này lấy nội dung của tệp hình ảnh và lưu nó vào cột BLOB của MySQL dưới dạng dữ liệu nhị phân. .
Tải lên tệp hình ảnh
Nhận thuộc tính hình ảnh (dữ liệu hình ảnh, loại hình ảnh, v.v. )
Chèn tệp hình ảnh vào BLOB

Làm cách nào để hiển thị hình ảnh BLOB được lưu trữ trong cơ sở dữ liệu MySQL?

Đầu tiên bạn cần bỏ câu lệnh if(isset($_GET['id']) vì muốn hiển thị tất cả ảnh. $query = mysql_query("chọn * từ `blob`");