The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 9 of 9

Thread: Displaying images from a mysql database

  1. #1

    Displaying images from a mysql database

    Hello all, php newbie here. I've set up a table in my database which holds images along with a title and description for each image. I'm trying to now display the images along with their relative titles and descriptions, but all I get is a page full of strange characters. This is the first time I've ever tried to display images from a db so any help would be appreciated. I'm attaching a screenshot of my table from phpMyAdmin for your reference. Thanks!

    JPearson311
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	imageDB.jpg 
Views:	19 
Size:	64.7 KB 
ID:	45371  

  2. #2
    An easier way to do it would be to upload the image to a directory, then just save the image src to the database. Then when calling the image from the database, you just call its location and echo that inside an img tag like this:
    <img src="<? echo $dbase_image_src ?>">

    That's how I do it anyway. If it is more complicated, you can always write the php code to create different directories for different users and include that inside the database src as well.

    If you are deteremined to use blobs, then I can't help you as I've never had much luck with them

  3. #3
    Ok, well how would I upload it to a directory, have the img src be placed in the db, and then display. To display would I use mysql_fetch_array? Thanks!

    JPearson311

  4. #4
    Here is a script I found on the interweb somewhere, it's helped me out loads. What it does is first check that the form has been submitted, if it hasn't it shows the form at the bottom.

    Once the form has been submitted it connects to the database using the database connection file, checks there is a file to upload, then checks that the filesize is under the limit. If that is OK, it checks that the file is of jpg, jpeg or gif format and if everything is OK uploads the file to the folder you select, here it is ../images/, but you need to change that according to where the files are going - remember if you are not uploading from the base directory you need to include ../ before the folder.

    The information to be stored in the database comes from $image, so if, like here you are uploading from a folder other than the base folder into another folder on the same level but will be showing the images from the base level, $image needs to be different from the folder you copy the files to, if the upload is from the base folder, you can keep them both the same.

    Hope this helps, if not give me a shout

    PHP Code:
    <? include'database_connection_file.php'//change this to suit yours//
    $maxsize=228480//set the max upload size in bytes
    if (!$HTTP_POST_VARS['submit']) {
      
    //print_r($HTTP_POST_FILES);
      
    $error=" ";
    //this will cause the rest of the processing to be skipped
    //and the upload form displays
    }
    if (!
    is_uploaded_file($HTTP_POST_FILES['picture']['tmp_name']) AND
    !isset(
    $error)) {
      
    $error "<b>You must upload a file!</b><br><br>";
      
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if (
    $HTTP_POST_FILES['picture']['size'] > $maxsize AND !isset($error)) {
      
    $error "<b>Error, file must be less than $maxsize bytes.</b><br><br>";
      
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if(
    $HTTP_POST_FILES['picture']['type'] != "image/gif" AND
    $HTTP_POST_FILES['picture']['type'] != "image/pjpeg" AND
    $HTTP_POST_FILES['picture']['type'] !="image/jpeg" AND !isset($error)) {
      
    $error "<b>You may only upload .gif or .jpeg files.</b><br><br>";
      
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if (!isset(
    $error)) {
    copy($HTTP_POST_FILES['picture']['tmp_name'],"../images/".$HTTP_POST_FILES
    ['picture']['name']);
      
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
      
    $image = ("images/" $HTTP_POST_FILES['picture']['name']);
    $image_title$_POST["image_title"];

      
    $result mysql_query("INSERT INTO YOUR DATABASE NAME (TABLE1, TABLE2) VALUES ('$image, '$image_title)") or die("Can't Perform Query");
    if (
    $result) echo "Success!! Thank you for your upload.";
      exit;
    }
    else
    {
      echo (
    "$error");
    }
    ?>        
            <form enctype="multipart/form-data" action="<?=$PHP_SELF?>" method="post">
    <table align="center" width="700" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td>Title (Image Name):</td>
        <td><INPUT TYPE="TEXT" NAME="image_title" SIZE=30></td>
      </tr>
      <tr>
        <td>Picture:<br>Pictures must be in .jpg or .gif format and<br>maximum size 640 pixels wide by 480 pixels high.</td>
        <td><input type="file" name="picture" /></td>
      </tr>
    </table>
          <div align="center"><input type="submit" name="submit" value="Submit"></div>
       
          </form>

  5. #5
    To display this way, yes you would need to do a fetch_array and echo the row in the img src tag, like this

    $query = mysql_query("SELECT * FROM table1);
    while ($r=mysql_fetch_array($query)) {
    $image=$r["your_image_row_name"];
    $image_title=$r["your_image_title_row_name"];

    echo "<img src='". $image ."'>";
    echo $image_title ;
    }

    And that's it! It gets more complicated if you have a lot of information to show, but that's the general idea.

  6. #6
    Ok, I'm able to get the image copied to the folder, but I can't get it to store the info into the db. When I test the form, I get "Can't Perform Query", but the image uploads to the folder.

    Here is my page:

    Code:
    <?php 
    $db_host = "localhost";
    $db_user = "myusername";
    $db_pwd = "mypwd";
    $db_name = "mydb";
    mysql_connect($db_host, $db_user, $db_pwd) or die("Could not connect to database");
    mysql_select_db($db_name);
    
    $maxsize=228480; //set the max upload size in bytes
    
    if (!$HTTP_POST_VARS['submit']) {
    //print_r($HTTP_POST_FILES);
    $error=" ";
    //this will cause the rest of the processing to be skipped
    //and the upload form displays
    }
    if (!is_uploaded_file($HTTP_POST_FILES['picture']['tmp_name']) AND
    !isset($error)) {
    $error = "<b>You must upload a file!</b><br><br>";
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if ($HTTP_POST_FILES['picture']['size'] > $maxsize AND !isset($error)) {
    $error = "<b>Error, file must be less than $maxsize bytes.</b><br><br>";
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if($HTTP_POST_FILES['picture']['type'] != "image/pjpeg" AND
    $HTTP_POST_FILES['picture']['type'] !="image/jpeg" AND !isset($error)) {
    $error = "<b>You may only upload .jpeg files.</b><br><br>";
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    }
    if (!isset($error)) {
    copy($HTTP_POST_FILES['picture']['tmp_name'],"images/".$HTTP_POST_FILES['picture']['name']);
    unlink($HTTP_POST_FILES['picture']['tmp_name']);
    $image = ("images/" . $HTTP_POST_FILES['picture']['name']);
    $image_title= $_POST["image_title"];
    $description = $_POST["description"];
    
    $result = mysql_query("INSERT INTO mydb (mytable) VALUES ('$image, '$image_title, '$description)") or die("Can't Perform Query");
    if ($result) echo "Success!! Thank you for your upload.";
    exit;
    }
    else
    {
    echo ("$error");
    }
    ?> 
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Rent-A-Shed :: Portable Storage Containers</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <script src="../Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
    </head>
    
    <body>
    <div id="container"><img src="../images/logo.png" alt="Rent-A-Shed" width="190" height="45" border="0" class="logo" />
    <div id="left">
    <h1>Website Purchase Page</h1>
    <p>Use the form below to add containers that you have for sale to the purchase page of your website.</p>
    <form action=<?php echo $PHP_SELF; ?> method="post" name="purchase" enctype="multipart/form-data">
    <p>Enter a title:</p>
    <input name="image_title" type="text" class="inputs" id="title" />
    <br /><br />
    <p>Enter a description:</p>
    <textarea name="description" cols="" rows="" id="description"></textarea><br /><br />
    <p>Click browse to choose an image to upload:</p> <span class="reminder">* Images must be .jpg format and no larger than 640x480</span>
    <input name="picture" type="file" class="picture" />
    <br /><br />
    <input name="submit" type="submit" class="submit" id="submit" value="Submit" />
    </form>
    </div>
    <div id="footer"><p>&copy; 2007 BJ Equipment, Inc All Rights Reserved</p></div>
    </div>
    </body>
    </html>

  7. #7
    PHP Code:
    $result mysql_query("INSERT INTO mytable VALUES ('$image', '$image_title', '$description')") or die("Can't Perform Query"); 
    Let us live so that when we come to die even the undertaker will be sorry. - Mark Twain
    Don't PM me your CSS, xHTML, JS or PHP questions. I will not reply to ANY IE6 questions.

  8. #8
    your sql is wrong:

    "INSERT INTO mydb (mytable) VALUES ('$image, '$image_title, '$description)""INSERT INTO mydb (mytable) VALUES ('$image, '$image_title, '$description)"

    should be "INSERT INTO mydb (mytable) VALUES ('$image', '$image_title', '$description')"

  9. #9
    This still isn't working even with the code previous code fixes.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012