Insert image in sql table and what is its dimension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sajithamol

    #1

    Insert image in sql table and what is its dimension

    How to insert a image in a table. wat is the dimension to be mentioned?
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

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

    Code:
    CREATE TABLE `images` (
      `id` int(11) NOT NULL auto_increment,
      `image` blob NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM;
    Then read the image file, encode and store in the BLOB field as in the example below.

    [PHP]$image = chunk_split(bas e64_encode(file _get_contents(" image.jpg")));
    $qry = "INSERT INTO images (image) VALUES('$image' )";
    mysql_query($qr y) or die(mysql_error ());[/PHP]

    And to display the image, use this code

    [PHP]header('Content-type: image/jpeg');
    $rs = mysql_fetch_arr ay(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?

    Comment

    Working...