Jump to content

calling images from MYSQL database! please help


englishcodemonkey

Recommended Posts

Ok i have a database called "image" with which i am storing images in blob format.i am currently trying to display the images that are in the database in this table. this is my current code for index.php:

<!DOCTYPE HTML PUBLIC 
  "-//W3C//DTD HTML 4.0 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <title>Browse Upload Files</title>
</head>
<body bgcolor="white">

<?php
  include 'db.inc';
  
  $query = "SELECT id, shortName, mimeName FROM image";

  /*if (!($connection = @ mysql_pconnect($hostName, 
                                    $username,
                                    $password)))
     showerror();

  if (!mysql_select_db("files", $connection))
     showerror();
        
  if (!($result = @ mysql_query ($query, $connection)))
     showerror();*/
?>
    <h1>Image database</h1> 

    <h3>Click <a href="insert.php">here</a> to 
upload an image.</h3>
<?php 
  //require 'disclaimer';

  if ($row = @ mysqli_fetch_array($result))
  {
?>

    <table>
    <col span="1" align="right">
    <tr>
       <th>Short description</th>
       <th>File type</th>
       <th>Image</th>

    </tr>
<?php
   do 
   {
?>
    <tr>
       <td><?php echo "{$row["shortName"]}";?></td>         
       <td><?php echo "{$row["mimeName"]}";?></td>
       <td><?php echo "<img src=\"view.php?file={$row["id"]}\">";?></td>
    </tr>
<?php
   } while ($row = @ mysqli_fetch_array($result));
?>
    </table>
<?php
  } // if mysql_fetch_array()
  else
     echo "<h3>There are no images to display</h3>\n";
?>
</body>
</html>

db.inc:

<?php

// These are the DBMS credentials
$hostName = "****";
$username = "****";
$password = "****";

// Show an error and stop the script
function showerror()
{
   if (mysqli_error())
      die("Error " . mysqli_errno() . " : " . mysqli_error());
   else
      die("Could not connect to the DBMS");
}

// Secure the user data by escaping characters 
// and shortening the input string
function clean($input, $maxlength)
{
  $input = substr($input, 0, $maxlength);
  $input = EscapeShellCmd($input);
  return ($input);
}

?>

and view.php:

<?php
  include 'db.inc';

  $file = clean($file, 4);

  if (empty($file))
     exit;

  if (!($connection = @ mysqli_pconnect($hostName,
                                       $username,
                                       $password)))
     showerror();

  if (!mysqli_select_db("files", $connection))
     showerror();

  $query = "SELECT mimeType, fileContents FROM image 
            WHERE id = $file";

  if (!($result = @ mysqli_query ($query,$connection)))
     showerror();  

  $data = @ mysqli_fetch_array($result);

  if (!empty($data["fileContents"]))
  {
    // Output the MIME header
     header("Content-Type: {$data["mimeType"]}");
    // Output the image
     echo $data["fileContents"];
   }
?>

when i upload this to my host and view the index.php file it says there are no images to display, but i have images there!

Thanks and sorry if this is a stupid mistake, just need another pair of eyes to take a look! thanks!

ok so i'm trying another way still with no luck, i have the "<img src="index.php" />" on one page and then the code for index.php is below:

<?php

$img = "P7180104.JPG";

$q = "SELECT fileContents from image where id='1'";

$r = @mysqli_query ($dbc, $q);

$imagepath="$r";

$image=imagecreatefromjpeg($imagepath);

header('Content-Type: image/gif');

imagejpeg($image);

?>

 

if i just type the filepath after "$imagepath" then it will display the image, but it will not find the file path from the database. i'm thinking maybe i have performed the query incorrectly? is "where id='1'" correct?

Thanks

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.