

Then, we can select the PDF data and render it in the web browser as follows: $a = $blobObj->selectBlob( 2) Įcho $a Code language: PHP ( php ) $blobObj->insertBlob( 'pdf/php-mysql-blob.pdf', "application/pdf") Code language: PHP ( php ) The following code inserts the content of the pdf/php-mysql-blob.pdf PDF file into the BLOB column: $blobObj = new BlobDemo() Then, we can select the BLOB data and display it as a GIF image: $a = $blobObj->selectBlob( 1) Įcho $a Code language: PHP ( php ) PHP MySQL BLOB with PDF files $blobObj->insertBlob( 'images/php-mysql-blob.gif', "image/gif") Code language: PHP ( php ) PHP MySQL BLOB with image filesįirst, we insert binary data from the images/php-mysql-blob.gif file into the BLOB column of the files table as follows: $blobObj = new BlobDemo() In the following examples, we will use the BlobDemo class to save a GIF image and a PDF file into the BLOB column of the files table.

*/ public function insertBlob ($filePath, $mime) Code language: PHP ( php ) PHP MySQL BLOB examples See the following insertBlob() method: /** Third, bind the file handle to the prepared statement using the bindParam() method and call the execute() method to execute the query.First, open the file for reading in binary mode.To insert the content of a file into a BLOB column, you follow the steps below: PHP PDO provides a convenient way to work with BLOB data using the streams and prepare statements. In the _construct() method, we open a database connection to the MySQL database, and in the _destruct() method, we close the connection. close the database connection $this->pdo = null for prior PHP 5.3.6 //$conn->exec("set names utf8") Second, we define a class called BlobDemo with the following code: pdo = new PDO($conStr, self::DB_USER, self::DB_PASSWORD) ) Code language: SQL (Structured Query Language) ( sql ) The following CREATE TABLE statement creates the files table: CREATE TABLE files ( The data column whose data type is the BLOB that is used to store the content of the file.The mime column stores the mime type of the file.The id column is the primary key, auto-increment column.

Let’s see how PHP PDO handles the BLOB type in MySQL.įirst, we create a new table named files in the sample database for practicing. You can change the communication package size by using the max_allowed_packet variable in MySQL and post_max_size in the PHP settings. The maximum value of a BLOB object is specified by the available memory and the communication package size. BLOB stands for the binary large data object. MySQL provides a BLOB type that can hold a large amount of data. Sometimes, for security reasons, you may need to store large data objects, e.g., images, PDF files, and videos, in the MySQL database.
