Connecting Tech Pros Worldwide Forums | Help | Site Map

Insert image in sql table and what is its dimension

sajithamol
Guest
 
Posts: n/a
#1: Jun 28 '07
How to insert a image in a table. wat is the dimension to be mentioned?

mwasif's Avatar
Moderator
 
Join Date: Jul 2006
Location: Pakistan
Posts: 719
#2: Jun 28 '07

re: Insert image in sql table and what is its dimension


Create a table with BLOB field, a sample table can be this

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `images` (
  2.   `id` int(11) NOT NULL auto_increment,
  3.   `image` blob NOT NULL,
  4.   PRIMARY KEY  (`id`)
  5. ) ENGINE=MyISAM;
Then read the image file, encode and store in the BLOB field as in the example below.

[PHP]$image = chunk_split(base64_encode(file_get_contents("image .jpg")));
$qry = "INSERT INTO images (image) VALUES('$image')";
mysql_query($qry) or die(mysql_error());[/PHP]

And to display the image, use this code

[PHP]header('Content-type: image/jpeg');
$rs = mysql_fetch_array(mysql_query("SELECT image from images where id=1"));
echo base64_decode($rs["image"]);[/PHP]

But make sure there must not be any sort of output before header().

Why you want to store the images in the DB?
Reply


Similar MySQL Database bytes