How to insert an image to MySql database with php

A BLOB is a binary large object that can hold a variable amount of data. There’re 4 datatypes that you can select for a blob field, depending how much storage of an image you’ll need.

TINYBLOB
A binary large object column with a maximum length of 255 (2^8 – 1) characters.

BLOB
A binary large object column with a maximum length of 65535 (2^16 – 1) characters.

MEDIUMBLOB
A binary large object column with a maximum length of 16777215 (2^24 – 1) characters.

LONGBLOB
A binary large object column with a maximum length of 4294967295 (2^32 – 1) characters.

This is the table that holds an image as a blob. You must select Blob datatype for the field ‘blob_img’

field type
id(5) unsigned
blob_img blob
text text

Now, on your php page (ex. insert.php), use this query to insert a real image to blob_img field in your table.

$file = file_get_contents('C:\Documents and Settings\Desktop\images\app_license_images.jpg');
$sql_query = 'insert into test.blob_tbl (blob_img, text) values($file, 'testing text here')';