Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to Output Image from Mysql?

    I can't seems to get the images from mysql table. All I get are strange numbers. When i add a header like this, it gives a warning like this,
    Warning: Cannot modify header information - headers already sent by.....

    Code:
        header("Content-type: image/jpeg");
    	echo $row['image']
    this is only a simple code to get the image out.
    Last edited by jhl84; 07-16-2007 at 08:47 AM. Reason: rephrase question

  • #2
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    we need to see a little more than that...

    However if it says 'headers already sent' and there are no other errors above that then likely your problem is 'whitespace'. Check to make sure there is nothing in your page above the <?php part No output can be sent to the browser before the 'header', including blank lines

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    but when i placed the header at the top of the page, i cannot get the html contents. i managed to view the image and it's only one image. it seems like i can't get any of the contents that i have.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,010
    Thanks
    2
    Thanked 312 Times in 304 Posts
    The only way to place an image on a web page and get a browser to render it is to use an <img src="a_url_that_outputs_an_image" alt=""> tag - http://w3schools.com/html/html_images.asp This is the only thing browsers understand. You need one <img...> tag for each image. The a_url_that_outputs_an_image needs to be to a separate .php file with your header/image echo output code in it.

    This is actually a HTML coding issue. If you search the forum and/or read through the PHP manual user contributed notes under the image functions you can find out more information.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Hm...I'm talking about extracting the image from mysql. If I just select from mysql db, it's some weird numbers. But when I add the header at the top of the page, I can't view any html contents.

  • #6
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    This may be a nitpick but....
    Why are you storing your pictures in the DB? It may be better for you if you were to instead store the path to the image opposed the image itself. That was you can retrieve the image path and dynamically write it in your src attribute.

    What data type are you using for the pic field? Long Blob?

    Also, you may find this post handy. http://forums.mysql.com/read.php?20,17671,27914
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    suddenly i figured storing in mysql may not be the best way. do u know of any sites where i can read up on storing in the server and path to mysql?

  • #8
    New Coder
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wrong stuufff
    Last edited by zain1992; 07-17-2007 at 06:22 AM. Reason: wrong script

  • #9
    New Coder
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hopefully this answers your question. i made this script for you.

  • #10
    New Coder
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    also wrong studd
    Last edited by zain1992; 07-17-2007 at 06:22 AM. Reason: wrong stuff

  • #11
    New Coder
    Join Date
    Jul 2007
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK THIS IS THE FINAL VERSION:

    I checked it and it works: here you go: USE THIS SCRIPT NOT THE ONE ABOVE:

    PHP Code:
    <?php 
    //Make variable for image  url to be inputed 
    $img_url $_POST['imageurl']; 
    $img_name $_POST['imagename']; 
    //Then connect to database. 

    $con mysql_connect("localhost""user""pass"); 
    if (!
    mysql_connect("localhost""user""pass")) 

    echo 
    "Could not connect"
    echo 
    mysql_error(); 


    //Then you choose the table to save info on 

    mysql_select_db(test$con);

    $sql "INSERT INTO Table_Name (Img_name, Img_url) VALUES ('$img_name', '$img_url')"

    if (!
    mysql_query($sql,$con)) 

        die(
    'Could not add image Data. Report this error to the Administrator:' mysql_error()); 

    Echo 
    "Added Image Data:<br>"
    Echo 
    "<table border=1><tr><td>Image URL: </td>"
    Echo 
    "<td>" $img_url "</td></tr>"
    Echo 
    "<tr><td>Image name: </td>"
    Echo 
    "<td>" $img_name "</td></tr>"
    Echo 
    "</table>"

    mysql_close($con); 
    ?> 
    <html> 
    <head><Title>Add image to database</title></head> 
    <body> 
    <h2>Add image data:</h2><br> 
    <form action="addimg.php" method="POST"> 
    <table border="1"> 
    <tr><td>Image URL: </td><td><input type="text" name="imageurl"></td></tr> 
    <tr><td>Image Name: </td><td><input type="text" name="imagename"></td></tr> 
    </table> 
    <input type="Submit" Value="Add Image Data"> 
    </form> 
    </body> 
    </html>
    Last edited by zain1992; 07-17-2007 at 06:22 AM. Reason: fixing

  • #12
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    but i'm confused as to how i should use the image header?

  • #13
    Senior Coder rafiki's Avatar
    Join Date
    Aug 2006
    Location
    Floating around somewhere...
    Posts
    2,042
    Thanks
    19
    Thanked 42 Times in 42 Posts
    you only need to use the image header if your using <img scr="image.php" alt...... /> and the headers go into image.php other wise use normal/no headers with <img scr="image.jpg" />

  • #14
    New Coder
    Join Date
    May 2006
    Posts
    59
    Thanks
    2
    Thanked 0 Times in 0 Posts
    i want to view the image from mysql db. once i query n select all the image from mysql, i need to add a header so that the image can be displayed correctly. but how do place/use header("Content-type: image/jpeg"); correctly? if i placed it at the top of the page, i can only get one image from mysql and the rest i don't know why i can't get it out. and also the html contents cannot be displayed. if i remove the header, the mysql image becomes broken and i get the html contents.

    how do i actually use this header correctly?

  • #15
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    You cannot do what you're trying to do and just keep asking the same question wont change the answer.
    Headers tell the browser what content to expect, sending a header of "content-type: image/jpeg" tells the browser to expect a single jpg image. You cant just mix it in with html, the browser is no longer expecting html, its expecting image data.

    The best solution to your problem is to store the image path in the database instead of the actual image


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •