how to store image in Mysql database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hemashiki
    New Member
    • Aug 2006
    • 34

    how to store image in Mysql database

    hi
    i need help..how can i store image in mysql database
    and i want to retrive that image
    can anyone suggest plz
  • stephane
    New Member
    • Feb 2007
    • 35

    #2
    there is BLOB datatype in Mysql

    to put it in, read image file with file_get_conten ts() function and put content into mysql query

    Comment

    • Motoma
      Recognized Expert Specialist
      • Jan 2007
      • 3236

      #3
      Originally posted by stephane
      there is BLOB datatype in Mysql

      to put it in, read image file with file_get_conten ts() function and put content into mysql query
      Thank you for your response stephane!

      Comment

      • hemashiki
        New Member
        • Aug 2006
        • 34

        #4
        [code=php]
        <form name="form1" enctype="multip art/form-data" method="post" action="">
        <input name="file1" type="file" id="file1">
        <input type="submit" name="Submit" value="Submit">
        </form>
        <?
        $f=$_POST['file1'];
        mysql_connect(" localhost","roo t","");
        mysql_select_db ("real");
        mysql_query("se lect * from reg");
        //$file_u=$_POST['file1'];



        $val = '0x' . bin2hex($f);
        $qu="insert into `reg`(`file`)va lues('$val')";

        mysql_query($qu ) or die("Query Error");
        print '<h1 align="center"> File Successfully Uploaded</h1>';
        ?>
        [/code]


        Here i inserted the image in database.its stored as BLOB 2BYTES
        and i tried to retrived by using this code
        [code=php]
        <?
        mysql_connect(" localhost","roo t","");
        mysql_select_db ("real");
        $q=mysql_query( "select * from reg");
        while($log=mysq l_fetch_array($ q))
        {

        print file_get_conten ts($log['file']);
        }
        ?>
        [/code]
        i dint get the image.....
        Last edited by Atli; Nov 17 '07, 02:04 AM. Reason: Added [code] tags.

        Comment

        • stephane
          New Member
          • Feb 2007
          • 35

          #5
          Originally posted by hemashiki
          while($log=mysq l_fetch_array($ q))
          {
          print file_get_conten ts($log['file']);
          }

          .
          why u use file_get_conten ts here?
          i think it must looks like this
          Code:
          while($log=mysql_fetch_array($q))
          {
          print $log['file'];
          }
          and don't foget to send image/jpg ( or other ) before

          Comment

          • Motoma
            Recognized Expert Specialist
            • Jan 2007
            • 3236

            #6
            Originally posted by hemashiki
            {

            print file_get_conten ts($log['file']);
            }
            This is wrong, first, you need to specify the content type: jpeg, gif, png, etc...
            Second, you aren't opening a file, so file_get_conten ts() should not be used.

            You will want to do something more like this:
            Code:
            {
            header("Content-type: image/png");
            echo $log['file'];
            }
            Hope this helps.

            Comment

            • hemashiki
              New Member
              • Aug 2006
              • 34

              #7
              Here i tried...
              print $log['file'];
              but the result came as OX
              plz give me an solution

              Comment

              • Nert
                New Member
                • Nov 2006
                • 64

                #8
                Originally posted by hemashiki
                Here i tried...
                print $log['file'];
                but the result came as OX
                plz give me an solution
                Hi hemashiki,

                according to you, you have already save image to database right? now you want to retrieve the image?
                here try this code, i've come up with this code when i encountered the same problem as yours.

                [code=php]mysql_connect(" localhost","roo t","");
                mysql_select_db ("real");
                $q=mysql_query( "select * from reg") or die(mysql_error ());
                $log = mysql_fetch_obj ect($q);
                header("Content-type: image/GIF");
                echo $log->file; [/code] hope this would help you too.

                --nert (^_^)

                Comment

                • stephane
                  New Member
                  • Feb 2007
                  • 35

                  #9
                  Originally posted by hemashiki
                  Here i tried...
                  print $log['file'];
                  but the result came as OX
                  plz give me an solution
                  because you insert to base at the same hex format
                  $val = '0x' . bin2hex($f);
                  it's wrong, you must insert simply file content, returned file_get_conten ts function

                  Comment

                  Working...