Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

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"));

Tạo một bảng để lưu trữ hình ảnh theo byte
Tạo Cơ sở dữ liệu với bảng cụ thể dưới dạng >> tạo hình ảnh cơ sở dữ liệu; >> tạo bảng lưu trữ hình ảnh (id int not null auto increment, primary key(id), name varchar(50) , image longblob , size int );
Kết quả -------->>Bảng được tạo thành công

HTML và CSS được thêm vào bằng PHP
Lưu trữ tệp trong một tên. Tải ảnh lên. php


Upload - photo



Please Select a file'; } else { try { $msg = upload(); // function calling to upload an image echo $msg; } catch(Exception $e) { echo $e->getMessage(); echo 'Sorry, Could not upload file'; } } function upload() { include "database/dbco.php"; $maxsize = 10000000; //set to approx 10 MB //check associated error code if($_FILES['userfile']['error']==UPLOAD_ERR_OK) { //check whether file is uploaded with HTTP POST if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { //checks size of uploaded image on server side if( $_FILES['userfile']['size'] < $maxsize) { $finfo = finfo_open(FILEINFO_MIME_TYPE); //checks whether uploaded file is of image type if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) { // prepare the image for insertion $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); // put the image in the db... // database connection $host = '127.0.0.1'; $user = 'root'; $pass = 'your_password'; $db = 'database_name'; mysql_connect($host, $user, $pass) OR DIE (mysql_error()); // select the db mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error()); // our sql query $sql = "INSERT INTO storeimages (id,image, name,size) VALUES ('$_POST[id]','{$imgData}', '{$_FILES['userfile']['name']}','{$_FILES['userfile']['size']}');"; mysql_query($sql) or die("Error in Query insert: " . mysql_error()); // insert the image $msg='

Image successfully saved in database .

'; } else $msg="

Uploaded file is not an image.

"; } else { // if the file is not less than the maximum allowed, print an error $msg='

File exceeds the Maximum File limit

Maximum File limit is '.$maxsize.' bytes

File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. ' bytes


'; } } else $msg="File not uploaded successfully."; } else { $msg= file_upload_error_message($_FILES['userfile']['error']); } return $msg; } // Function to return error message based on error code function file_upload_error_message($error_code) { switch ($error_code) { case UPLOAD_ERR_INI_SIZE: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; case UPLOAD_ERR_FORM_SIZE: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; case UPLOAD_ERR_PARTIAL: return 'The uploaded file was only partially uploaded'; case UPLOAD_ERR_NO_FILE: return 'No file was uploaded'; case UPLOAD_ERR_NO_TMP_DIR: return 'Missing a temporary folder'; case UPLOAD_ERR_CANT_WRITE: return 'Failed to write file to disk'; case UPLOAD_ERR_EXTENSION: return 'File upload stopped by extension'; default: return 'Unknown upload error'; } } ?>


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

Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

Đô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

________số 8_______

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 đến 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)
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

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 dữ liệu BLOB vào MySQL bằng PHP?
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

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 dữ liệu BLOB vào MySQL bằng PHP?
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

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 dữ liệu BLOB vào MySQL bằng PHP?
Làm cách nào để chèn dữ liệu BLOB vào MySQL bằng PHP?

Để 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 dữ liệu blob trong 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

Làm cách nào để tải lên dữ liệu blob trong PHP?

For displaying BLOB images to the browser, create a PHP file and to do the following. This file will then be used as the tag source to display the images into the browser. Get image data stored with the MySQL BLOB field in the database. Set the content-type as image (image/jpg, image/gif, …)

Làm cách nào để tạo BLOB trong PHP?

Để chèn nội dung của tệp vào cột BLOB, bạn làm 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 bindParam() và gọi phương thức exec() để thực hiện truy vấn

Kiểu dữ liệu BLOB trong PHP là gì?

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.