Page 1 of 1

image display from mysql Database

Posted: Thu Oct 07, 2010 9:55 am
by oreo
I am a beginner programmer looking to display images from mysql database.
I have tried many way and googled a lot but still i can not make this work.

The output i am getting is the alt attribute in my database.
my picture has these attributes in the database: piccontent picname pictype picsize
my data type is blob and the id i am working with is 47 as this is the id the picture is in on my images table on my database.
in the main html file i called a php file like this:
<img alt="Your Picture Here" height="106" src="get_image.php" width="126">

in get_image.php i wrote the following:

Code: Select all

<?php
 	require_once("connection.php");
 
	$query = "SELECT picname, pictype, picsize, piccontent FROM images WHERE id = '47'";
	$result = mysql_query($query) or die('Error, query failed');

	$data = @MYSQL_RESULT($result,0,"piccontent");
	$type = @MYSQL_RESULT($result,0,"pictype");
	$size = @MYSQL_RESULT($result,0,"picsize");

header('Content-type: ' . $type);
echo $data
?>

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 11:22 am
by mikosiko
first... remove those @'s they are preventing you to see any error those sentences can trigger.

I also suggest you to include this 2 lines at the beginning of your code after your <?php line, its will allow you to see any error that your code could be generate.

Code: Select all

ini_set("display_errors", "1");
error_reporting(E_ALL);
as an alternative, you probably should try to replace this

Code: Select all

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

        $data = @MYSQL_RESULT($result,0,"piccontent");
        $type = @MYSQL_RESULT($result,0,"pictype");
        $size = @MYSQL_RESULT($result,0,"picsize");
with something like this:

Code: Select all

$result = mysql_query($query)  or die('Error, query failed');;
if($row = mysql_fetch_assoc($result)){
  $data = $row['piccontent'];
  $type = $row['pictype'];
  $size = $row['picsize'];
}

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 11:36 am
by oreo
Following your suggestions i have changed my code to the following, the result is the same as before.

Code: Select all

<?php
	ini_set("display_errors", "1");
	error_reporting(E_ALL);
 	require_once("connection.php");
 	require_once("functions.php"); 

	$query = "SELECT picname, pictype, picsize, piccontent FROM carpetclean WHERE id = '47'";
	$result = mysql_query($query) or die('Error, query failed');
if($row = mysql_fetch_assoc($result)){
  $data = $row['piccontent'];
  $type = $row['pictype'];
  $size = $row['picsize'];
}

header('Content-type: ' . $type);
echo $data

?>

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 11:45 am
by twinedev
If you are getting the "Alt" attribute, and that is not even a fields you are selecting from the database (based on your naming conventions), then the problem maybe what actually put the image in the database.

-Greg

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 11:52 am
by oreo
I am getting the user to fill in a form and then upload an image to my database.
My sqladmin shows the correct file name, correct file type and correct file content that were uploaded to the database.
This is the code to insert the image to database:

Code: Select all

	$fileName = $_FILES['userfile']['name'];
	$tmpName  = $_FILES['userfile']['tmp_name'];
	$fileSize = $_FILES['userfile']['size'];
	$fileType = $_FILES['userfile']['type'];

	if (!($fp=fopen($tmpName, 'rb'))) echo "file faild to open" ;
	$content = fread($fp, filesize($tmpName));
	$content = addslashes($content);
	fclose($fp);

	if(!get_magic_quotes_gpc())
	{
	    $fileName = addslashes($fileName);
	}	
	$query = "INSERT INTO carpetclean (piccontent,picname,pictype,picsize) VALUES ('$content','$fileName','$fileType','$fileSize')";
		    if(!mysql_query ($query))
		    {
		    	  die('Error: ' . mysql_error());
		    }
}



Re: image display from mysql Database

Posted: Thu Oct 07, 2010 12:55 pm
by mikosiko
oreo wrote:Following your suggestions i have changed my code to the following, the result is the same as before.
......
header('Content-type: ' . $type);
echo $data

?>
[/syntax]
you have a missing ; in you echo

and if that doesn't fix the problem then please explain what exactly do you mean by "The output i am getting is the alt attribute in my database." and also please echo the content of your $type variable

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 1:24 pm
by oreo
I added the ; to the end and still no luck.
the output i am getting is a broken picture and the ALT in the <img> tag.
i echo the type and got : image/jpeg
i echo the size and got: 74049
i echo the data without the header and got lots of signs and gibrish.

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 1:31 pm
by oreo
it seems when i remove the header type i can echo the signs and any other data i want. Once i put the content type i only see a the broken pic sign and the ALT attribute from my image tag.
again the content type is image/jpeg

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 5:27 pm
by mikosiko
ok... forget your code for a while... create a new file with this code... replace in the define() sentences DB_USER, DB_PASSWORD and DB_DATABASE with your own information.

Code: Select all

<?php
  // Define how to display/report errors
   ini_set("display_errors", "1");
   error_reporting(E_ALL);
   if (isset($_GET['tid'])) {
      $id = $_GET['tid'];
   }  else {
     die("Error");
   }

   // Define Constants for DB Access
   define('DB_HOST', 'localhost');
   define('DB_USER', 'yourusername');
   define('DB_PASSWORD', 'yourpassword');
   define('DB_DATABASE', 'yourdatabase');
   // Try to connect to the DB
   $link = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_DATABASE);
    $query = "SELECT pictype, piccontent FROM carpetclean WHERE id = $id";
    $result = mysql_query($query) or die('Error, query failed');
   $row = mysql_fetch_assoc($result);
$data = $row['piccontent'];
$type = $row['pictype'];
header('Content-type: $type');
echo $data;
?>
call this file testimage.php per example
and execute it directly from your browser in this way
localhost/testimage.php?tid=47
or adjust to your setup... it should work... I did test it ok.

if this work you more likely have an error in your code... either a header problem or a syntax error that you are no seeing.

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 8:32 pm
by oreo
I think i should try a different approach.
I did some reading and found people suggested to save the file on the server and save the path on the DB.
I tried to do that now instead and i manage to save the file but i saved it without slashes so when i tried to display i can not.
i will try some code and i hope i can get it to work.

Thank you!!!

Re: image display from mysql Database

Posted: Thu Oct 07, 2010 8:53 pm
by mikosiko
oreo wrote:I think i should try a different approach.
I did some reading and found people suggested to save the file on the server and save the path on the DB.
I tried to do that now instead and i manage to save the file but i saved it without slashes so when i tried to display i can not.
i will try some code and i hope i can get it to work.

Thank you!!!
and that should be your best option indeed

Re: image display from mysql Database

Posted: Fri Oct 08, 2010 12:11 am
by oreo
seems i can't get a break, i again manage to load the file, save the url path but not be able to view the file.

here is how to try to read and display the file:

Code: Select all


                $id = 52;
		$query = "SELECT name, type, size, filepath FROM upload WHERE id = '$id'";
		$result = mysql_query($query) or die('Error, query failed');
		list($name, $type, $size, $filePath) = mysql_fetch_array($result);
		header("Content-length: $size");
		header("Content-type: $type");
		readfile($filePath);	
		exit;
}