Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to retrieve and display an image from MySQL using php

  1. #1
    Join Date
    Jun 2009
    Posts
    14

    Default How to retrieve and display an image from MySQL using php

    Hey guyz,
    I stored some pictures in MySQL db through php, but i don't know how to display them!!I tried to echo the image Variable "$image" but it shows me something like this:

    JFIFC       C  ``" ĵ}!1AQa"q2#BR$ 3br %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz  ĵw!1AQaq"2B #3Rbr $4%&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvw xyz ?"K|K>$pXciG;V 탁4G_0FD$MYKNtjr[ڎC㦞!!Ԃt~T0*j7#Mj3Tm eݤK[ B?

    then it displays the other fields, first,last,date


    Any help would be greatly appreciated, this is my Code

    display.php

    <html>
    <head>
    <style type="text/css">
    table {font-family: Calibri; font-size: 12pt;font-weight:bold;}
    </style>
    </head>
    <body>
    <form action = "info.php"><input name="imagesubmit"type ="image" value="back" src="back.png"></form>
    <?php

    $user = "root";
    $pwd = "";
    $host = "localhost";
    $dbname = "s2hdatabase";

    $id = $_POST['view'];



    $conn = mysql_connect($host,$user,$pwd) or ("<br>[en] ERROR on MySQL server connection");

    mysql_select_db($dbname,$conn);

    $query="SELECT * FROM Player WHERE ID= '$id'";
    $result=mysql_query($query,$conn);

    $num=mysql_numrows($result);

    mysql_close($conn);

    echo "<b><center><image src='playerinfo.jpg' /></center></b><br><br>";




    $first=mysql_result($result,$i,"First_Name");
    $last=mysql_result($result,$i,"Last_Name");
    $img=mysql_result($result,$i,"img");

    $line=0;
    function bg($i)
    {
    if ($i % 2 ==0)
    {
    return '#CCFFCC';
    }
    else
    {
    return '#FFFFCC';
    }
    }


    echo "<table width = 100% border = '0' cellspacing = '0' cellpadding = '0'>";
    if ($first != ""){
    echo "<tr bgcolor= '".bg($line++)."'> <td align='LEFT' ><div align='Left'><strong>First name:</strong></div></td>
    <td align='LEFT'><div align='Left'><strong>$first</strong></div></td> </tr>";
    }
    if($last != ""){
    echo "<tr bgcolor= '".bg($line++)."'><td align='RIGHT' ><div align='Left'><strong>Last name:</strong></div></td>
    <td align='RIGHT'><div align='Left'><strong>$last</strong></div></td> </tr>";
    }
    if($date != "0000-00-00"){
    echo "<tr bgcolor= '".bg($line++)."'><td align='RIGHT' ><div align='Left'><strong>Birth date:</strong></div></td>
    <td align='RIGHT'><div align='Left'><strong>$date</strong></div></td> </tr>";
    }

    else
    echo"$img";

    echo "</table>";
    ?>
    </body>
    </html>

  2. #2
    Join Date
    Jan 2008
    Location
    cardboard box
    Posts
    389

    Default

    You have to output the header of the image type before the image data, and all of it needs to be isolated from any other output.

    Again, storing the image in a database is ill-advised.
    Have you tried turning it off and on again?

  3. #3
    Early Out's Avatar
    Early Out is offline Former Moderator, Still Respected
    Join Date
    Mar 2006
    Location
    Sector R
    Posts
    4,643

    Default

    Quote Originally Posted by wysiwyg View Post
    Again, storing the image in a database is ill-advised.
    Right on target. There's a good reason why image-storage scripts like Coppermine and Zenphoto use the database to organize the photo collection, but don't use the database to store the images themselves.

  4. #4
    Join Date
    Jun 2009
    Posts
    14

    Default

    Thanks guyz for your help, but i am new in php, can you please tell me how to output the header? or actually how the code should be? i knew that i could save the image path on MySQL and put the images in a folder, but it didn't work also...
    Last edited by Samo; 07-10-2009 at 01:42 PM.

  5. #5
    Join Date
    May 2007
    Posts
    17

    Default

    Instead of us trying to help you out with putting your image in the database, why don't you try posting your code for uploading the image to a directory and referencing the path in the database.

    Since most people tend to agree that storing images in the database is going to cause more problems than it is worth it will be beneficial for us to help you try and do it the other way instead of promote storing images in the database.

    If you really cannot get around storing them in the database you would need a second php script to pull the image out of the database and then send the appropriate content type and then print the results. I can provide examples of this if you really need but I would rather show you how to do it the other way and help you get it done as others have suggested.

  6. #6

    Default I'd like to know

    I am building a web site where users store images online. I was going to store the pictures in a MYSQL database. I would be interested in the problems with that and learning the way you recommend.

    Thanks.
    Matt

    My life can be summed up in one word, "indescribable".

  7. #7

    Default

    It is not difficult and you can store them in the database. But you need to take note of 2 things.

    1. You have to make sure you store the picture in your database as a BINARY field. That means everything store in the database is exactly the image binary data.

    2. You CANNOT output the image straight away in an HTML file. Instead you need to use another PHP file to generate just the image. The code is simple

    header("Content-type: image/gif"); // take note of the format, jpeg? png?
    echo $image;
    Say the above PHP file is named "pullimage.php?image=new1.jpg"

    Then only you put this URL in your HTML/PHP page file

    <img src="pullimage.php?image=new1.jpg" />
    If you get the idea, it is easy.
    Read my complete BlueHost Review to know why it is highly recommended!

  8. #8
    Join Date
    Nov 2006
    Location
    Sydney, Australia
    Posts
    4,944

    Default

    BlueHost support staff have actually recommended against storing images in databases as doing so uses significantly more of the limited resources than storing them as ordinary images would.

  9. #9
    Join Date
    May 2007
    Posts
    17

    Default

    As felgall has stated it is very likely that if you store your images in the database that you could run into problems with your hosting account using to many resources.

    Jamesyap has the right idea if you are trying to accomplish storing the image in the database however you should really try to get it working by storing the image in a directory on your account and referencing it in the database.

    All this takes is upload the image to a directory that is visible on the internet, when you create your insert statement into MySQL instead of storing the binary data you just store the path to the image.

    For example:

    Let's say you upload the image player.jpg. You have created a folder call profile_images in the same directory you have your php script. You would want to upload the image to 'profile_images/player.jpg' and then reference this path inside of your MySQL insert statment.

    Then when you go to display the image you would do something like the following:

    Code:
    $user = "root";
    $pwd = "";
    $host = "localhost";
    $dbname = "s2hdatabase";
    
    $id = $_POST['view'];
    
    $conn = mysql_connect($host,$user,$pwd) or ("<br>[en] ERROR on MySQL server connection");
    mysql_select_db($dbname,$conn);
    
    $query="SELECT * FROM Player WHERE ID= '$id'";
    $result=mysql_query($query,$conn);
    
    $first=mysql_result($result,$i,"First_Name");
    $last=mysql_result($result,$i,"Last_Name");
    $img_path=mysql_result($result,$i,"img_path");
    
    echo "<b><center><image src='$img_path' /></center></b><br><br>";
    
    echo "<table width = 100% border = '0' cellspacing = '0' cellpadding = '0'>";
    if ($first != ""){
    echo "<tr bgcolor= '".bg($line++)."'> <td align='LEFT' ><div align='Left'><strong>First name:</strong></div></td>
    <td align='LEFT'><div align='Left'><strong>$first</strong></div></td> </tr>";
    }
    if($last != ""){
    echo "<tr bgcolor= '".bg($line++)."'><td align='RIGHT' ><div align='Left'><strong>Last name:</strong></div></td>
    <td align='RIGHT'><div align='Left'><strong>$last</strong></div></td> </tr>";
    }
    This will save you a lot of problems from the start and again if you have problems doing it this way then post the code that you are using and we can try to help you from there.

  10. #10
    Join Date
    Jun 2009
    Posts
    14

    Default

    This is how i upload my images to MySQL

    this is the form that calls insert.php
    <html>
    <body>
    <form enctype="multipart/form-data" action="insert.php" method="post">
    <table border= 0 cellpadding="1" cellspacing="0"class="txt" align="left" >

    <tr bgcolor = "lightgreen">
    <td align="RIGHT" class="bText" >
    <div align="right"><strong>Player</strong></div></td>
    <td bgcolor="lightgreen"></td>
    </tr>


    <tr>
    <td align="RIGHT" class="bText">
    <div align="right"><strong>First name:</strong></div></td>
    <td onMouseOver="this.className='borderOn'" onMouseOut="this.className='borderOff'" class="borderOff"> <b>
    <input type="TEXT" name="Text1" >

    </b></tr> <!-- input type = text-->

    <input name="MAX_FILE_SIZE" value="2000000" type="hidden">
    <input name="image" accept="image/jpeg" type="file">

    </table>
    </form>
    </body>
    </html>

    and this is insert.php

    <html>
    <?php
    $user = "root";
    $pwd = "";
    $host = "localhost";
    $dbname = "s2hdatabase";


    if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

    // Temporary file name stored on the server
    $tmpName = $_FILES['image']['tmp_name'];

    // Read the file
    $fp = fopen($tmpName, 'r');
    $data = fread($fp, filesize($tmpName));
    $data = addslashes($data);
    fclose($fp);
    }
    else
    echo"<br><b>You didn't select an image to upload</br>";

    $conn = mysql_connect($host,$user,$pwd) or ("<br>[en] ERROR on MySQL server connection");

    $t1 = $_POST[Text1];
    $t2 = $_POST[Text2];

    mysql_select_db($dbname,$conn);

    $sql3 = "INSERT INTO Player(First_Name, Last_Name,img)
    VALUES('$t1', '$t2','$data')";

    if this way is probably going to cause problems as you said, can you guyz plz tell me how to do it with storing the image path in MySQL.

Tags for this Thread

Posting Permissions

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