how to insert, update and select blob field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
piseypeng
Forum Newbie
Posts: 2
Joined: Mon Oct 24, 2005 10:41 pm

how to insert, update and select blob field

Post by piseypeng »

Hello All,

I just start using PHP and MYSQL and I don't know how to Insert, update and select data from blob field.

Can somebody give me a sample code of how to insert a picture to blob field and code to select it back.

I am appreciate your help. Thanks.

Pisey
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's just like working with any other field type..

Code: Select all

$filedata = file_get_contents('filename.jpg');
mysql_query('INSERT INTO `table` (`field`) VALUES(\'' . mysql_real_escape_string($filedata) . '\')');

// ...

$query = mysql_query('SELECT `field` FROM `table`');


However, it is highly recommended to not store images in a database. Main reasons: to display an image, you must have a script be able to pull a single image from the database and return the binary data. A page will reference this script via an image tag. This operation increases server overhead needlessly (most often). As an example, if you want to show multiple images on a page, each image will have to reference this image script to request a single image, each one will require the database server to unpack the binary data, transfer it to php, then php outputs it to the web server's buffers..
Post Reply