{"id":2905,"date":"2013-06-09T19:20:00","date_gmt":"2013-06-10T03:20:00","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=2905"},"modified":"2024-01-14T23:04:44","modified_gmt":"2024-01-15T06:04:44","slug":"php-mysql-blob","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/","title":{"rendered":"PHP MySQL BLOB"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to handle BLOB data using PHP PDO. We will show you how to insert, update, and select BLOB data in MySQL databases.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"alignright\"><img loading=\"lazy\" decoding=\"async\" width=\"195\" height=\"144\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif\" alt=\"PHP MySQL Blob\" class=\"wp-image-2929\" title=\"PHP MySQL Blob\"\/><\/figure>\n<\/div>\n\n\n<p>Sometimes, for security reasons, you may need to store large data objects such as images, PDF files,\u00a0and\u00a0videos, in the MySQL database.<\/p>\n\n\n\n<p>MySQL provides the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-blob\/\">BLOB data type<\/a> that can store a large amount of binary data. <\/p>\n\n\n\n<p>BLOB stands for the binary large data object. The maximum value of a BLOB object is specified by the available memory and the communication package size. You can change the communication package size by using the&nbsp; <code>max_allowed_packet<\/code> variable in MySQL and&nbsp; <code>post_max_size<\/code> in the PHP settings.<\/p>\n\n\n\n<p>Let&#8217;s see how PHP PDO handles the BLOB type in MySQL.<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">create a new table<\/a> named <code>files<\/code> in for practice.<\/p>\n\n\n\n<p>The <code>files<\/code> table contains three columns:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>id<\/code> column is the <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-primary-key\/\">primary key<\/a>, <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-auto_increment\/\">auto-increment<\/a> column.<\/li>\n\n\n\n<li>The <code>mime<\/code> column stores the mime type of the file.<\/li>\n\n\n\n<li>The <code>data<\/code> column whose data type is the BLOB used to store the content of the file.<\/li>\n<\/ul>\n\n\n\n<p>The following <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">CREATE TABLE<\/a> statement creates the <code>files<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> files (\n    <span class=\"hljs-keyword\">id<\/span>   <span class=\"hljs-built_in\">INT<\/span>           AUTO_INCREMENT PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    mime <span class=\"hljs-built_in\">VARCHAR<\/span> (<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    <span class=\"hljs-keyword\">data<\/span> <span class=\"hljs-built_in\">BLOB<\/span>          <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define a class called <code>BlobDemo<\/code> with the following code:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n<span class=\"hljs-comment\">\/**\n * PHP MySQL BLOB Demo\n *\/<\/span>\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BlobDemo<\/span> <\/span>{\n\n    <span class=\"hljs-keyword\">const<\/span> DB_HOST = <span class=\"hljs-string\">'localhost'<\/span>;\n    <span class=\"hljs-keyword\">const<\/span> DB_NAME = <span class=\"hljs-string\">'classicmodels'<\/span>;\n    <span class=\"hljs-keyword\">const<\/span> DB_USER = <span class=\"hljs-string\">'root'<\/span>;\n    <span class=\"hljs-keyword\">const<\/span> DB_PASSWORD = <span class=\"hljs-string\">''<\/span>;\n\n    <span class=\"hljs-comment\">\/**\n     * Open the database connection\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__construct<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-comment\">\/\/ open database connection<\/span>\n        $conStr = sprintf(<span class=\"hljs-string\">\"mysql:host=%s;dbname=%s;charset=utf8\"<\/span>, <span class=\"hljs-keyword\">self<\/span>::DB_HOST, <span class=\"hljs-keyword\">self<\/span>::DB_NAME);\n\n        <span class=\"hljs-keyword\">try<\/span> {\n            <span class=\"hljs-keyword\">$this<\/span>-&gt;pdo = <span class=\"hljs-keyword\">new<\/span> PDO($conStr, <span class=\"hljs-keyword\">self<\/span>::DB_USER, <span class=\"hljs-keyword\">self<\/span>::DB_PASSWORD);\n            <span class=\"hljs-comment\">\/\/for prior PHP 5.3.6<\/span>\n            <span class=\"hljs-comment\">\/\/$conn-&gt;exec(\"set names utf8\");<\/span>\n        } <span class=\"hljs-keyword\">catch<\/span> (PDOException $e) {\n            <span class=\"hljs-keyword\">echo<\/span> $e-&gt;getMessage();\n        }\n    }\n\n    <span class=\"hljs-comment\">\/**\n     * close the database connection\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">__destruct<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n        <span class=\"hljs-comment\">\/\/ close the database connection<\/span>\n        <span class=\"hljs-keyword\">$this<\/span>-&gt;pdo = <span class=\"hljs-keyword\">null<\/span>;\n    }\n\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the <code> __construct()<\/code> method, we <a href=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-connecting-to-mysql-database\/\">open a database connection to the MySQL database<\/a>, and in the&nbsp; <code>__destruct()<\/code> method, we close the connection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Insert BLOB data into the database<\/h2>\n\n\n\n<p>PHP PDO provides a convenient way to work with BLOB data using the streams and prepare statements.<\/p>\n\n\n\n<p>To insert the content of a file into a BLOB column, you follow the steps below:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, open the file for reading in binary mode.<\/li>\n\n\n\n<li>Second, construct an <a title=\"MySQL INSERT\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">INSERT statement<\/a>.<\/li>\n\n\n\n<li>Third, bind the file handle to the prepared statement using the &nbsp;<code>bindParam()<\/code> method and call the&nbsp; <code>execute()<\/code> method to execute the query.<\/li>\n<\/ul>\n\n\n\n<p>See the following&nbsp; <code>insertBlob()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">   <span class=\"hljs-comment\">\/**\n     * insert blob into the files table\n     * <span class=\"hljs-doctag\">@param<\/span> string $filePath\n     * <span class=\"hljs-doctag\">@param<\/span> string $mime mimetype\n     * <span class=\"hljs-doctag\">@return<\/span> bool\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">insertBlob<\/span><span class=\"hljs-params\">($filePath, $mime)<\/span> <\/span>{\n        $blob = fopen($filePath, <span class=\"hljs-string\">'rb'<\/span>);\n\n        $sql = <span class=\"hljs-string\">\"INSERT INTO files(mime,data) VALUES(:mime,:data)\"<\/span>;\n        $stmt = <span class=\"hljs-keyword\">$this<\/span>-&gt;pdo-&gt;prepare($sql);\n\n        $stmt-&gt;bindParam(<span class=\"hljs-string\">':mime'<\/span>, $mime);\n        $stmt-&gt;bindParam(<span class=\"hljs-string\">':data'<\/span>, $blob, PDO::PARAM_LOB);\n\n        <span class=\"hljs-keyword\">return<\/span> $stmt-&gt;execute();\n    }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Notice that the&nbsp;<code>PDO::PARAM_LOB<\/code> instructs PDO to map the data as a stream.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Update an existing BLOB column<\/h2>\n\n\n\n<p>To update a BLOB column, you use the same technique as described in inserting data into a BLOB column. See the following&nbsp; <code>updateBlob()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">   <span class=\"hljs-comment\">\/**\n     * update the files table with the new blob from the file specified\n     * by the filepath\n     * <span class=\"hljs-doctag\">@param<\/span> int $id\n     * <span class=\"hljs-doctag\">@param<\/span> string $filePath\n     * <span class=\"hljs-doctag\">@param<\/span> string $mime\n     * <span class=\"hljs-doctag\">@return<\/span> bool\n     *\/<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">updateBlob<\/span><span class=\"hljs-params\">($id, $filePath, $mime)<\/span> <\/span>{\n\n        $blob = fopen($filePath, <span class=\"hljs-string\">'rb'<\/span>);\n\n        $sql = <span class=\"hljs-string\">\"UPDATE files\n                SET mime = :mime,\n                    data = :data\n                WHERE id = :id;\"<\/span>;\n\n        $stmt = <span class=\"hljs-keyword\">$this<\/span>-&gt;pdo-&gt;prepare($sql);\n\n        $stmt-&gt;bindParam(<span class=\"hljs-string\">':mime'<\/span>, $mime);\n        $stmt-&gt;bindParam(<span class=\"hljs-string\">':data'<\/span>, $blob, PDO::PARAM_LOB);\n        $stmt-&gt;bindParam(<span class=\"hljs-string\">':id'<\/span>, $id);\n\n        <span class=\"hljs-keyword\">return<\/span> $stmt-&gt;execute();\n    }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Query data from the BLOB column<\/h2>\n\n\n\n<p>The following steps describe how to select data from a BLOB column:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, construct a <a title=\"MySQL SELECT statement\" href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-select-from\/\">SELECT statement<\/a>.<\/li>\n\n\n\n<li>Second, bind the corresponding parameter using the&nbsp; <code>bindColumn()<\/code> method of the <code>PDOStatement<\/code> object.<\/li>\n\n\n\n<li>Third, execute the statement.<\/li>\n<\/ul>\n\n\n\n<p>See the following&nbsp; <code>selectBlob()<\/code> method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">    <span class=\"hljs-comment\">\/**\n     * select data from the the files\n     * <span class=\"hljs-doctag\">@param<\/span> int $id\n     * <span class=\"hljs-doctag\">@return<\/span> array contains mime type and BLOB data\n     *\/<\/span>\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">selectBlob<\/span><span class=\"hljs-params\">($id)<\/span> <\/span>{\n\n        $sql = <span class=\"hljs-string\">\"SELECT mime,\n                        data\n                   FROM files\n                  WHERE id = :id;\"<\/span>;\n\n        $stmt = <span class=\"hljs-keyword\">$this<\/span>-&gt;pdo-&gt;prepare($sql);\n        $stmt-&gt;execute(<span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\":id\"<\/span> =&gt; $id));\n        $stmt-&gt;bindColumn(<span class=\"hljs-number\">1<\/span>, $mime);\n        $stmt-&gt;bindColumn(<span class=\"hljs-number\">2<\/span>, $data, PDO::PARAM_LOB);\n\n        $stmt-&gt;fetch(PDO::FETCH_BOUND);\n\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">array<\/span>(<span class=\"hljs-string\">\"mime\"<\/span> =&gt; $mime,\n            <span class=\"hljs-string\">\"data\"<\/span> =&gt; $data);\n    }<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">PHP MySQL BLOB examples<\/h2>\n\n\n\n<p>In the following examples, we will use the <code>BlobDemo<\/code> class to save a GIF image and a PDF file into the BLOB column of the <code>files<\/code> table.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PHP MySQL BLOB with image files<\/h3>\n\n\n\n<p>First, we insert binary data from the <code>images\/php-mysql-blob.gif<\/code> file into the BLOB column of the <code>files<\/code> table as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$blobObj = <span class=\"hljs-keyword\">new<\/span> BlobDemo();\n\n<span class=\"hljs-comment\">\/\/ test insert gif image<\/span>\n$blobObj-&gt;insertBlob(<span class=\"hljs-string\">'images\/php-mysql-blob.gif'<\/span>,<span class=\"hljs-string\">\"image\/gif\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"169\" height=\"41\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-gif-example.png\" alt=\"php mysql blob gif example\" class=\"wp-image-2909\" title=\"php mysql blob gif example\"\/><\/figure>\n\n\n\n<p>Then, we can select the BLOB data and display it as a GIF image:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$a = $blobObj-&gt;selectBlob(<span class=\"hljs-number\">1<\/span>);\nheader(<span class=\"hljs-string\">\"Content-Type:\"<\/span> . $a&#91;<span class=\"hljs-string\">'mime'<\/span>]);\n<span class=\"hljs-keyword\">echo<\/span> $a&#91;<span class=\"hljs-string\">'data'<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"397\" height=\"211\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-gif-image.png\" alt=\"php mysql blob render gif image\" class=\"wp-image-2910\" title=\"php mysql blob render gif image\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-gif-image.png 397w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-gif-image-300x159.png 300w\" sizes=\"auto, (max-width: 397px) 100vw, 397px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">PHP MySQL BLOB with PDF files<\/h3>\n\n\n\n<p>The following code inserts the content of the&nbsp; <code>pdf\/php-mysql-blob.pdf<\/code> PDF file into the BLOB column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$blobObj = <span class=\"hljs-keyword\">new<\/span> BlobDemo();\n\n<span class=\"hljs-comment\">\/\/ test insert pdf<\/span>\n$blobObj-&gt;insertBlob(<span class=\"hljs-string\">'pdf\/php-mysql-blob.pdf'<\/span>,<span class=\"hljs-string\">\"application\/pdf\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"196\" height=\"61\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-pdf-example.png\" alt=\"php mysql blob pdf example\" class=\"wp-image-2911\" title=\"php mysql blob pdf example\"\/><\/figure>\n\n\n\n<p>Then, we can select the PDF data and render it in the web browser as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$a = $blobObj-&gt;selectBlob(<span class=\"hljs-number\">2<\/span>);\nheader(<span class=\"hljs-string\">\"Content-Type:\"<\/span> . $a&#91;<span class=\"hljs-string\">'mime'<\/span>]);\n<span class=\"hljs-keyword\">echo<\/span> $a&#91;<span class=\"hljs-string\">'data'<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"442\" height=\"236\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-PDF.png\" alt=\"php mysql blob render PDF\" class=\"wp-image-2912\" title=\"php mysql blob render PDF\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-PDF.png 442w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/php-mysql-blob-render-PDF-300x160.png 300w\" sizes=\"auto, (max-width: 442px) 100vw, 442px\" \/><\/figure>\n\n\n\n<p>To replace the PDF file with the GIF image file, you use the&nbsp; <code>updateBlob()<\/code> method as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$blobObj-&gt;updateBlob(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'images\/php-mysql-blob.gif'<\/span>, <span class=\"hljs-string\">\"image\/gif\"<\/span>);\n\n$a = $blobObj-&gt;selectBlob(<span class=\"hljs-number\">2<\/span>);\nheader(<span class=\"hljs-string\">\"Content-Type:\"<\/span> . $a&#91;<span class=\"hljs-string\">'mime'<\/span>]);\n<span class=\"hljs-keyword\">echo<\/span> $a&#91;<span class=\"hljs-string\">'data'<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can download the source code of this tutorial via the following link:<\/p>\n\n\n\n<p><a class=\"buttonDownload\" href=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2018\/03\/phpmysqlblob.zip\" target=\"_blank\" rel=\"noopener noreferrer\">Download PHP MySQL BLOB Source Code<\/a><\/p>\n\n\n\n<p>In this tutorial, you have learned how to manage MySQL BLOB\u00a0data, including inserting, updating, and querying blob.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful? <\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"2905\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/\"\n\t\t\t\tdata-post-title=\"PHP MySQL BLOB\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"2905\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/\"\n\t\t\t\tdata-post-title=\"PHP MySQL BLOB\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.<\/p>\n","protected":false},"author":2,"featured_media":2929,"parent":1268,"menu_order":8,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-2905","page","type-page","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP MySQL BLOB<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP MySQL BLOB\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-15T06:04:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"195\" \/>\n\t<meta property=\"og:image:height\" content=\"144\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/\",\"name\":\"PHP MySQL BLOB\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-BLOB.gif\",\"datePublished\":\"2013-06-10T03:20:00+00:00\",\"dateModified\":\"2024-01-15T06:04:44+00:00\",\"description\":\"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-BLOB.gif\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2013\\\/06\\\/PHP-MySQL-BLOB.gif\",\"width\":195,\"height\":144,\"caption\":\"PHP MySQL Blob\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/php-mysql-blob\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP MySQL Tutorial\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/php-mysql\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PHP MySQL BLOB\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP MySQL BLOB","description":"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/","og_locale":"en_US","og_type":"article","og_title":"PHP MySQL BLOB","og_description":"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.","og_url":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/","og_site_name":"MySQL Tutorial","article_modified_time":"2024-01-15T06:04:44+00:00","og_image":[{"width":195,"height":144,"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif","type":"image\/gif"}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/","url":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/","name":"PHP MySQL BLOB","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif","datePublished":"2013-06-10T03:20:00+00:00","dateModified":"2024-01-15T06:04:44+00:00","description":"In this tutorial, you will learn how to handle BLOB data using PDO including insert, updating, and reading BLOB.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2013\/06\/PHP-MySQL-BLOB.gif","width":195,"height":144,"caption":"PHP MySQL Blob"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/php-mysql\/php-mysql-blob\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"PHP MySQL Tutorial","item":"https:\/\/www.mysqltutorial.org\/php-mysql\/"},{"@type":"ListItem","position":3,"name":"PHP MySQL BLOB"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=2905"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2905\/revisions"}],"predecessor-version":[{"id":14155,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/2905\/revisions\/14155"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/1268"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media\/2929"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=2905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}