Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?

Tìm hiểu cách MySQL Maestro có thể giúp bạn nhận ra MySQL 5 mới. 1 tính năng trong thực tế. Ví dụ chi tiết được bao gồm

Tìm hiểu tất cả các tính năng mới của MySQL 5 (Giao dịch ACID, Thủ tục và chức năng được lưu trữ, Trình kích hoạt, Chế độ xem, v.v. ) và cách MySQL Maestro có thể giúp bạn sử dụng chúng dễ dàng và hiệu quả trong thực tế từ bài viết mới của chúng tôi. Ví dụ được bao gồm

RazorSQL chứa một công cụ cho phép người dùng làm việc với MySQL blobs, dữ liệu nhị phân và dữ liệu hình ảnh. Các tùy chọn bao gồm khả năng chỉnh sửa dữ liệu blob, lưu dữ liệu blob vào tệp và xem dữ liệu blob dưới dạng hình ảnh

Để khởi chạy trình chỉnh sửa đốm màu MySQL, hãy kết nối với MySQL, sau đó xem nội dung của bảng chứa cột nhị phân hoặc đốm màu bằng cách sử dụng tùy chọn xem nội dung của trình duyệt cơ sở dữ liệu hoặc bằng cách chạy truy vấn chọn đối với bảng. Sau khi kết quả được hiển thị trong phần kết quả truy vấn, nhấp chuột phải/menu ngữ cảnh của ô kết quả chứa dữ liệu nhị phân sẽ chứa các tùy chọn được gọi là "Trình chỉnh sửa dữ liệu nhị phân" và "Xem dưới dạng hình ảnh"

Để chỉnh sửa dữ liệu nhị phân hoặc lưu dữ liệu nhị phân vào một tệp, có thể chọn tùy chọn "Trình chỉnh sửa dữ liệu nhị phân". Lưu ý, tùy chọn này chỉ khả dụng cho các bảng có khóa chính. Sau khi chọn mục trình đơn trình soạn thảo dữ liệu nhị phân, cửa sổ trình soạn thảo dữ liệu nhị phân sẽ hiển thị. Có các tùy chọn để lưu dữ liệu nhị phân vào tệp, cập nhật dữ liệu nhị phân từ tệp, xem dữ liệu dưới dạng hình ảnh hoặc chỉnh sửa dữ liệu dưới dạng dữ liệu ký tự sau khi chỉ định mã hóa ký tự

Để chèn dữ liệu nhị phân vào một blob hoặc cột nhị phân trong một hàng mới trong bảng cơ sở dữ liệu, trước tiên, một hàng phải được chèn có chứa giá trị null cho cột hoặc giá trị giả. Sau khi hàng mới được chèn vào, người dùng có thể truy vấn bảng bằng RazorSQL và khởi chạy trình chỉnh sửa dữ liệu nhị phân

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 để thay đổi dữ liệu BLOB trong MySQL?
Làm cách nào để thay đổi dữ liệu BLOB trong 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 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)
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 

/** * 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)
20 sau đây

/** * 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

Lưu ý rằ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)
21 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 

/** * 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)
22 sau đây

/** * 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

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 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)
    23 của đối tượ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)
    24
  • Thứ ba, thực hiện tuyên bố

Xem 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)
25 sau đây

/** * 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

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 ta chèn dữ liệu nhị phân từ tệ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)
28 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

/** * 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àm cách nào để thay đổi dữ liệu BLOB trong MySQL?
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?

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

/** * 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
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?

PHP MySQL BLOB với tệp PDF

Đoạn mã sau chèn nội dung của tệ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)
60 PDF vào cột BLOB

/** * 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
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?
Làm cách nào để thay đổi dữ liệu BLOB trong 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.

/** * 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
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?
Làm cách nào để thay đổi dữ liệu BLOB trong MySQL?

Để thay thế tệp PDF bằng tệp ảnh GIF, bạn sử dụng 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)
22 như 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)
1

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.

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 để đọc BLOB trong MySQL?

Đọc dữ liệu BLOB từ cơ sở dữ liệu MySQL . open a new connection to the database. Sau đó, xây dựng một câu lệnh SELECT và tạo một PreparedStatement từ đối tượng Connection. Cuối cùng, gọi các phương thức close() của các đối tượng PreparedStatment và Connection.

Làm cách nào để chèn dữ liệu vào cột BLOB trong MySQL?

Tài khoản người dùng MySQL có khả năng tạo cơ sở dữ liệu và bảng. .
Bước 1. Đăng nhập vào cơ sở dữ liệu MySQL của bạn. .
Bước 2. Chọn loại dữ liệu BLOB. .
Bước 3. Tạo cấu trúc cơ sở dữ liệu. .
Bước 4. Chèn dữ liệu vào bảng sinh viên. .
Bước 4. Lấy dữ liệu từ bảng sinh viên