<?php
session_start();
if (isset($_SESSION['username']))
{
include("database.php");
            $username = $_SESSION['username'];
            $query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND p.username='$username'";

        $result = mysql_query($query) or die(mysql_error());
        $patient = mysql_num_rows($result);
        if ($patient!=0)
        {
            while ($row = mysql_fetch_array($result))
            {
                $dMR_no = $row[0];
                $dusername = $row[1];
                $dpassword = $row[2];
                $demail = $row[3];
                $dlname = $row[5];
                $dfname = $row[6];
                $dgender = $row[11];
                $dmname = $row[7];
                $dage = $row[9];
                $dcontact_no = $row[12];
                $dlocation = $row[8];
            //  print_r($row);
            }
        }

        else
        {
            die(mysql_error());
        }

            <?php echo "<img src=showfile.php?id=$dMR_no>"; //this is the line of code that will display a php file that contains the code for retrieving the image
}
else
die("Please Login to view this page");
?>

this is the content of showfile.php

<?php
header("Content-type: image/jpeg");
include("database.php");
$id = $dMR_no;
$image = mysql_query("SELECT * FROM tblpatient_image WHERE RelationMR_no='$id'");
$image = mysql_fetch_assoc($image);
$image = stripslashes($image['image_file']);
echo $image;
?>

I want to display an image coming from the database to a webpage but it gives me a broken image icon and when I try to open it in new tab it says that the image cannot be displayed because it contains errors

What is stored in $image?
is that the binary-image-data or just a filename?

that is the field with the binary file

Usaly it faster to store just the filename in the db and create a link to the image it self.
But if you have to, try this:

<?php
header("Content-type: image/jpeg");
include("database.php");
//$id = $dMR_no;
$id= mysql_real_escape_string($_GET['id']);
$image = mysql_query("SELECT * FROM tblpatient_image WHERE RelationMR_no='$id'") or die(mysql_error()); // give you some error if it fails
$image = mysql_fetch_assoc($image);
// only use stripslashes if you added them when you saved the image to the db
$image = stripslashes($image['image_file']);
echo $image;
?>

At first I can see a long pixelated image but when I try it for the second etc. time even the broken image icon have gone.
What can be the cost of this? is it on the retrieval process or maybe on the saving process which made it corrupted?

I tried it again and it worked but the image is corrupted I see pixelated image but somehow I can still recognize its original looks

how is the image saved and stored in the db.
can you show that code?

this is the editprofile.php where I include the imagefrm.php that contains the code for saving the image:

<?php
session_start();
include("database.php");
if (isset($_SESSION['username']))
{
			$username = $_SESSION['username'];
			$query = "SELECT *
FROM tblpatient_pass p, tblpatient_info i
WHERE p.RelationMR_no = i.MR_no
AND p.username='$username'";

		$result = mysql_query($query) or die(mysql_error());
		$patient = mysql_num_rows($result);
		if ($patient!=0)
		{
			while ($row = mysql_fetch_array($result))
			{
				$dMR_no = $row[0];
				$dusername = $row[1];
				$dpassword = $row[2];
				$demail = $row[3];
				//print_r($row);  
			}
		}
		
		else
		{
			die(mysql_error());
		}

include('imagefrm.php'); // this is the include file code for the form where my codes for adding a picture can be found
}
else
die("Please Login to view this page");
?>

and this is the content of imagefrm.php

<?php
include("database.php");
$file = $_FILES['image']['tmp_name'];
if (isset($_POST['upload']))
{
//check if a file has been selected
	if (!isset($file))
	{
		$error_image = "No file has been selected!";
	}
	//if a file was selected
	else
	{
		$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
		$image_name = addslashes($_FILES['image']['name']);
		$image_size = getimagesize($_FILES['image']['tmp_name']);
		//check if the object is an image
		if ($image_size==FALSE)
		{
			$error_image = "Selected file is not an image!";
		}
		//if the file is an image
		else
		{
			$query = mysql_query("SELECT * from tblpatient_image WHERE RelationMR_no = '$dMR_no'") or die(mysql_error());
			$result = mysql_num_rows($query);
			//check if their is existing image of the current user in the database
			if ($result!=0)
			{
				$query = mysql_query("UPDATE tblpatient_image SET image_name = '$image_name', image_file = '$image'   WHERE RelationMR_no = '$dMR_no'");
				$error_image = "Profile picture has been updated successfuly!";
			}
			//add new image if no existing image for the user
			else
			{
				if (!$query = mysql_query("INSERT INTO tblpatient_image VALUES ('','$dMR_no','$image_name','$image')"))
				{
					$error_image = "Please select a valid file.";
				}
				else
				{
				$error_image = "Image has been uploaded successfully!";
				}
			}
		}
	}
}
else
{
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<center><h3>Change Profile Picture</h3></center>
<table cellspacing="10px" cellpadding:"10px">
<tr><td width="475"><waku>Please choose image from your disk to upload</waku></td></tr>
<tr><td>
<input name="image" type="file" style="background-color:#FFFFFF; font-size:16px; font-weight:500;" /></td></tr>
<tr><td><input type="submit" value="Upload Image" name="upload" style="background-color:#00EA3A; font-size:16px; font-weight:800; color:#FFFFFF;" /></td></tr>
<tr><td colspan="2"><?php echo $error_image; ?></td></tr></table>
</form>

looks fine to me
did some researce
apparently stripslashes() is not binary-save
but you can do without.
still use addslashes()
see example

have used myself:

$image = base64_encode(file_get_contents($_FILES['image']['tmp_name']));
// and
$image = base64_decode($image['image_file']);

one other point:
You include database.php twice
recoment you use

include_once('database.php');

in both cases

this codes worked:

$image = base64_encode(file_get_contents($_FILES['image']['tmp_name']));
// and
$image = base64_decode($image['image_file']);

thank you very much :D but can I ask another question? how can I resize the image that I will display in my web page, because all I need is 200px X 200px image resolution,

here is some code i used in one of my prodjects
it also conferts to .jpg
and does some errorchecking
you have to modify it to fit your code your self (and some better errormesages)
but i think it will get you going

// checking foto errors 
if ((!isset($_FILES['foto'])) || ($_FILES['foto']['error']==4)) 
    {
    $ErrorMessage='<p class="error">foto not found ';
    $FotoError=true;
    }  
elseif (($_FILES['foto']['error']==1) || ($_FILES['foto']['error']==2)) 
    {
    $ErrorMessage='<p class="error">foto to big</p>Max 3Mb';
    $FotoError=true;
    }
elseif ($_FILES['foto']['error']==3) 
    {
    $ErrorMessage='<p class="error">loading error </p>';
    $FotoError=true;
    }
elseif ($_FILES['foto']['error']>=6) 
    {
    $ErrorMessage='<p class="error">couldnot save (error: '.$_FILES['foto']['error'].')</p>';
    $FotoError=true;
    }
// checking foto type
elseif ($_FILES['foto']['type']!='image/gif' && $_FILES['foto']['type']!='image/jpeg' &&$_FILES['foto']['type']!='image/pjpeg' && $_FILES['foto']['type']!='image/png')
   {
   $ErrorMessage='<p class="error">Deze foto is geen jpg, gif of png</p>';
   $FotoError=true;
   }
else 
   {
   $Fototemp=$_FILES['foto']['tmp_name'];
   //  create image object
   switch(exif_imagetype($Fototemp))
	{
	case IMAGETYPE_GIF:
	    $Imageobj = @imagecreatefromgif($Fototemp);
	    If (!$Imageobj)
        	{
		$ErrorMessage='<p class="error">not a good gif</p>';
		$FotoError=true;
		}
	   break;
	case IMAGETYPE_JPEG:
	   $Imageobj = @imagecreatefromjpeg($Fototemp);
	   If (!$Imageobj)
	        {
		$ErrorMessage='<p class="error">not a good jpg</p>';
		$FotoError=true;
		}
	   break;
	case IMAGETYPE_PNG:
	   $Imageobj = @imagecreatefrompng($Fototemp);
	   If (!$Imageobj)
	        {
		$ErrorMessage='<p class="error"> not a  good  png</p>';
		$FotoError=true;
		}
	   break;
	default:
	   $ErrorMessage='<p class="error">this  fototype is not recognied</p>';
	   $FotoError=true;
	}
    if(!$FotoError)
	{
        // creacting new file name
	$Fotobase=basename($Fototemp);
	$Fotofile=substr($Fotobase,strpos($Fotobase,'.')).'.jpg';
	//redusing foto size
        $Imagewidth = imagesx($Imageobj);
	$Imageheight = imagesy($Imageobj);
	$Maxsize=200;
        if ($Imagewidth>$Imageheight)
            {
            $Height=($Imageheight * $Maxsize) / $Imagewidth;
	    $Width=  $Maxsize;
    	    }
        else
            {
            $Height=  $Maxsize;
            $Width=($Maxsize * $Imagewidth) / $Imageheight;
	    }
    	$Width = round($Width);
	$Height = round($Height);
  	$Smallobj = imagecreatetruecolor($Width, $Height);
	imagecopyresampled($Smallobj, $Imageobj, 0, 0, 0, 0, $Width, $Height, $Imagewidth, $Imageheight);
	imagejpeg($Smallobj, ($DirImagetemp.$Fotofile));
        imagedestroy($Smallobj);

foto now saved in file $Fotofile
but you probley want to do something like

// (untested)
$image = base64_encode($Smallobj);

It worked,, I realized that the only lines that I need is these:

$desired_width = 150;
$desired_height = 150;
$im = imagecreatefromstring($image);
$new = imagecreatetruecolor($desired_width, $desired_height);
$x = imagesx($im);
$y = imagesy($im);
imagecopyresampled($new, $im, 0, 0, 0, 0, $desired_width, $desired_height, $x, $y);
imagedestroy($im);
header('Content-type: <span class="posthilit">image</span>/jpeg');
imagejpeg($new, null, 85);

if you do it like this you have the big image in your db. a waste of diskspace and speed if you only use the small one

its ok,, because Im just testing it for now,, I will just improve it later on, :)

You may check the following php for image viewing & displaying": Using imageMagick:

<?php
    $handler = new Imagick($filename);
    $handler->setImageColorspace(imagick::COLORSPACE_RGB);
    $handler->writeImage();
?>

When i run this php file it syas that image cannot be displayed because it contains error.plz help me to solve this problem

<?php 
$query=mysql_connect("localhost","root","root");
mysql_select_db("freeze_demo",$query);include('phpgraphlib.php');
$graph = new PHPGraphLib(400,300);
$data_array=array();


$query1="SELECT name,age FROM addd WHERE name='pavi' ";
$query2 = mysql_query($query1) or die('Query failed: ' . mysql_error());
if ($query2) {

  while ($row = mysql_fetch_assoc($query2)) {

      $name=$row['name'];
      $age=$row['age'];

       $data_array[$name]=$age;
  }
}
$graph->addData($data_array);
$graph->setTitle("hello");
$graph->setGradient("lime", "green");
$graph->setBarOutlineColor("black");
$graph->createGraph();
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.