Results 1 to 10 of 10

Thread: PHP Display image out of DB

Threaded View

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    228

    PHP Display image out of DB

    hi guys

    I've hit another problem Got a db which holds the images I need for the site. There is a number of reasons for doing that and other options were checked out.

    I also got a script which supposed to show those images, unfortunately it only shows the first image in db unless specificaly told display another image, but I need it to desplay all the images stored or multiple entries.

    Here's the table settings:
    CREATE TABLE upload (
    id INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(30) NOT NULL,
    type VARCHAR(30) NOT NULL,
    size INT NOT NULL,
    content MEDIUMBLOB NOT NULL,
    PRIMARY KEY(id)
    );


    and here is the last variation of the code

    <?php
    $host = "localhost";
    $user = "";
    $password = "";
    $database = "";

    $connection = mysql_connect($host,$user,$password)
    or die ("Couldn't connect to a server");

    $database = mysql_select_db($database, $connection);


    $query = "SELECT id, type, content FROM upload";

    $result = mysql_query($query) or die('Error, query failed');

    if(mysql_num_rows($result) == 0)
    {
    echo "Database is empty <br>";
    }

    else
    {
    while($results = mysql_fetch_array($result))
    {
    $content = $results["content"];
    $type = $results["type"];
    $id = $results["id"];


    $image= imagecreatefromstring($content);

    switch ($type){

    case "image/jpeg":
    header("Content-type: image/jpeg");
    $display = imagejpeg ($image);
    imagedestroy($image);
    break;

    case "image/png":
    header("Content-type: image/png");
    $display = imagepng($image);
    imagedestroy($image);
    break;

    case "image/gif":
    header ("Content-type: image/gif");
    $display = imagegif($image);
    imagedestroy($image);
    break;

    }
    }
    }

    ?>


    <html>
    <head>
    <title>Download File From MySQL</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?
    for ($i=0; i<count($display); $i++){
    echo $id;
    echo' <img src="' . $display . '" /><br />';
    }


    ?>

    </body>
    </html>



    So far there are definately 3 images in db, they are all jpegs. Any help welcome
    Last edited by nightcat; March 13th, 2007 at 12:52 PM.
    Don\'t post if you\'ve got nothing constructive to say. Flooding is annoying

Similar Threads

  1. Tutorial: Apache 2.0.49 and PHP 4.3.7!
    By Vorlin in forum Other Tutorials Forum
    Replies: 3
    Last Post: June 15th, 2004, 10:25 PM
  2. The history of the Mac line of Operating systems
    By gore in forum Operating Systems
    Replies: 3
    Last Post: March 7th, 2004, 08:02 AM
  3. Installing Apache and PHP on Linux
    By HDD in forum Other Tutorials Forum
    Replies: 2
    Last Post: February 1st, 2004, 08:05 PM

Posting Permissions

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