Not able fetch the image from mysql database and to display it.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 7255 Views - Last Post: 14 January 2011 - 03:53 AM Rate Topic: -----

#1 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Not able fetch the image from mysql database and to display it.

Posted 10 January 2011 - 05:21 AM

I found a set of php code to insert an image into mysql database and also to fetch that image and to display. The insertion is being done successfully, but the displaying part is not working properly. Can find out the problem. Here is the code:
<?php 
// index.php - by Hermawan Haryanto &lt;hermawan@codewalkers.com&gt; 
// Example PHP Script, demonstrating Storing Image in Database 
// Detailed Information can be found at http://www.codewalkers.com 

// database connection 
$conn = mysql_connect("localhost", "username", "password") 
  OR DIE (mysql_error()); 
@mysql_select_db ("database", $conn) OR DIE (mysql_error()); 

// Do this process if user has browse the 
// file and click the submit button 
if ($_FILES) { 
  $image_types = Array ("image/bmp", 
                        "image/jpeg", 
                        "image/pjpeg", 
                        "image/gif", 
                        "image/x-png"); 
  if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”])) { 
    $userfile  = addslashes (fread 
                 (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                 filesize ($_FILES["userfile"]["tmp_name"]))); 
    $file_name = $_FILES["userfile"]["name"]; 
    $file_size = $_FILES["userfile"]["size"]; 
    $file_type = $_FILES["userfile"]["type"]; 

    if (in_array (strtolower ($file_type), $image_types)) { 
      $sql = "INSERT INTO image " 
             . "(image_type, image, image_size, image_name, image_date) "; 
      $sql.= "VALUES ("; 
      $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', " 
             . "'{$file_name}', NOW())"; 
      @mysql_query ($sql, $conn); 
      Header("Location:".$_SERVER["PHP_SELF"]); 
      exit(); 
    } 
  } 
} 

// Do this process of user has click 
// a file name to view or remove 
if ($_GET) { 
  $iid = $_GET["iid"]; 
  $act = $_GET["act"]; 
  switch ($act) { 
    case rem: 
      $sql = "DELETE FROM image WHERE image_id=$iid"; 
      @mysql_query ($sql, $conn); 
      Header("Location:./index.php"); 
      exit(); 
      break; 
    default: 
      print "&lt;img src="image.php?iid=$iid"&gt;"; 
      break; 
  } 
} 

?> 
<html> 
<head> 
<title>Storing Images in DB</title> 
</head> 
<body> 
<form method="post" enctype="multipart/form-data">
Select Image File: 
<input type="file" name="userfile"  size="40"> 
<input type="submit" value="submit"> 
</form> 
<?php 
  $sql = "SELECT * FROM image ORDER BY image_date DESC"; 
  $result = mysql_query ($sql, $conn); 
  if (mysql_num_rows($result)&gt;0) { 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
      $i++; 
      $str .= $i.". "; 
      $str .= "<a href='index.php?iid=".$row["image_id"]."'>" 
           . $row["image_name"]."</a> "; 
      $str .= "[".$row["image_date"]."] "; 
      $str .= "[".$row["image_size"]."] "; 
      $str .= "[<a href='index.php?act=rem&amp;iid=".$row["image_id"] 
           . "'>Remove</a>]<br>"; 
    } 
    print $str; 
  } 
?> 
</body> 
</html>

<?php 
// image.php - by Hermawan Haryanto &lt;hermawan@dmonster.com&gt; 
// Example PHP Script, demonstrating Storing Image in Database 
// Detailed Information can be found at http://www.codewalkers.com 

// database connection 
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error()); 
@mysql_select_db ("hermawan", $conn) OR DIE (mysql_error()); 
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
if (mysql_num_rows ($result)&gt;0) { 
  $row = @mysql_fetch_array ($result); 
  $image_type = $row["image_type"]; 
  $image = $row["image"]; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
?>



Is This A Good Question/Topic? 0
  • +

Replies To: Not able fetch the image from mysql database and to display it.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Not able fetch the image from mysql database and to display it.

Posted 10 January 2011 - 05:37 AM

Why don't you ask the author of your copy & paste code? His email's right in there.
Was This Post Helpful? 3
  • +
  • -

#3 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 10 January 2011 - 05:59 AM

Sure, I will try with the author. But still if anyone could find the bug they are welcome.
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Not able fetch the image from mysql database and to display it.

Posted 10 January 2011 - 06:16 AM

You have not adequately described your problem such that anyone can really provide you with help. "The displaying part is not working properly" is not a good description of your problem. How do you EXPECT the display to work, and how is it ACTUALLY working?
Was This Post Helpful? 0
  • +
  • -

#5 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 10 January 2011 - 09:39 PM

I mean to say that the php code to fetch the image from the database and to display it using <image> tag is not working i.e., the image is not getting displayed. I think now you can understand what the problem is.
Was This Post Helpful? 0
  • +
  • -

#6 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Not able fetch the image from mysql database and to display it.

Posted 11 January 2011 - 12:25 AM

Remove all the error suppression operators (@) and after establishing database connection and selecting database, try with the following code-

$sql = "SELECT * FROM image ORDER BY image_date DESC"; 
  $result = mysql_query ($sql, $conn) or die(mysql_error()); 



and see what happens, if it executes successfully, then check the number of rows returned-

echo mysql_num_rows($result);


This post has been edited by atik97: 11 January 2011 - 12:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 12 January 2011 - 10:03 PM

Atik, I tried to run the code by removing all the error suppression operators(@) as you suggested me. The below code is being executed successfully by fetching the result and printing the number of rows fetched:
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
print mysql_num_rows ($result);
But still the problem is it is not displaying the fetched image. I mean that the below code is not displaying the image.
if (mysql_num_rows ($result)&gt;0) { 
  $row = @mysql_fetch_array ($result); 
  $image_type = $row["image_type"]; 
  $image = $row["image"]; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 


Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

Re: Not able fetch the image from mysql database and to display it.

Posted 13 January 2011 - 03:15 AM

how do you call that second code?

PS. not sure if the &gt; is intended … if it is, change it to >

This post has been edited by Dormilich: 13 January 2011 - 03:17 AM

Was This Post Helpful? 1
  • +
  • -

#9 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 13 January 2011 - 06:12 AM

Dormilich, i have made the correction to the above code and used it with below code:
// database connection 
$conn = mysql_connect("localhost", "root", "") 
  OR DIE (mysql_error()); 
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error()); 

$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
print mysql_num_rows ($result);
if (mysql_num_rows ($result)>0) { 
  $row = mysql_fetch_object ($result); 
  $image_type = $row->image_type; 
  $image = $row->image; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
But still I could not get the output.
Was This Post Helpful? 0
  • +
  • -

#10 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Not able fetch the image from mysql database and to display it.

Posted 13 January 2011 - 07:41 AM

I highly doubt you have made any effort to make the script working, it seems just copy-paste of the code.

if (is_uploaded_file ($_FILES[“userfile”][“tmp_name”]))



This is just a sample of code with invalid character, There are same case in lots of areas in your code, html entities are being in use in the code you provided instead of special characters like "<", ">", "&". Correct those portions. I don't know if you have put all the code in same file or created two separate files. Actually bottom part will be place in separate file named image.php.

I will ask you to made another change in your index.php file.

$str .= "<a href='index.php?iid=".$row["image_id"]."'>"
077	           . $row["image_name"]."</a> "; 



Replace index.php with image.php, as image.php file is responsible for displaying image, not the index.php file. index.php file will only show link to display image or delete the image. Made all the corrections and let me know if it works or not. If you can made all the corrections i have mentioned, there is no reason to fail the script.
Was This Post Helpful? 1
  • +
  • -

#11 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 14 January 2011 - 01:09 AM

Though I am a beginner in PHP, I have common sense of programming. The code that that I posted earlier is the actual code posted by the author. And I have already removed all the invalid characters that you have mentioned above earlier than you have asked me to do. Also I have used 2 separate files "index.php" and "image.php". The modified code after removing all the invalid characters are as below: index.php
<?php // database connection 
$conn = mysql_connect("localhost", "root", "") 
  OR DIE (mysql_error()); 
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error()); 

// Do this process if user has browse the 
// file and click the submit button 
if ($_FILES) { 
  $image_types = Array ("image/bmp", 
                        "image/jpeg", 
                        "image/pjpeg", 
                        "image/gif", 
                        "image/x-png"); 
  if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) { 
    $userfile  = addslashes (fread 
                 (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                 filesize ($_FILES["userfile"]["tmp_name"]))); 
    $file_name = $_FILES["userfile"]["name"]; 
    $file_size = $_FILES["userfile"]["size"]; 
    $file_type = $_FILES["userfile"]["type"]; 

    if (in_array (strtolower ($file_type), $image_types)) { 
      $sql = "INSERT INTO image " 
             . "(image_type, image, image_size, image_name, image_date) "; 
      $sql.= "VALUES ("; 
      $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', " 
             . "'{$file_name}', NOW())"; 
      $res=mysql_query ($sql, $conn); 
      Header("Location:".$_SERVER["PHP_SELF"]); 
      exit(); 
    } 
  } 
} 

// Do this process of user has click 
// a file name to view or remove 
if ($_GET) { 
  $iid = $_GET["iid"]; 
  $act = $_GET["act"]; 
  switch ($act) { 
    case rem: 
      $sql = "DELETE FROM image WHERE image_id=$iid"; 
      $res1=mysql_query ($sql, $conn); 
      Header("Location: index.php"); 
      exit(); 
      break; 
    default: 
	Header("Location: image.php?iid=$iid");
      break; 
  } 
} 

?> 
<html> 
<head> 
<title>Storing Images in DB</title> 
</head> 
<body> 
<form method="post" enctype="multipart/form-data">
Select Image File: 
<input type="file" name="userfile"  size="40"> 
<input type="submit" value="submit"> 
</form> 
<?php 
  $sql = "SELECT * FROM image ORDER BY image_date DESC"; 
  $result = mysql_query ($sql, $conn); 
  if (mysql_num_rows($result)>0) { 
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
      $i++; 
      $str .= $i.". "; 
      $str .= "<a href='image.php?iid=".$row["image_id"]."'>" 
           . $row["image_name"]."</a> "; 
      $str .= "[".$row["image_date"]."] "; 
      $str .= "[".$row["image_size"]."] "; 
      $str .= "[<a href='index.php?act=rem&amp;iid=".$row["image_id"] 
           . "'>Remove</a>]<br>"; 
    } 
    print $str; 
  } 
?> 
</body> 
</html>

And the image.php is as follows:
<?php 
// image.php 

// database connection 
$conn = mysql_connect("localhost", "root", "") 
  OR DIE (mysql_error()); 
mysql_select_db ("justgetit", $conn) OR DIE (mysql_error()); 

$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"]; 
$result = mysql_query ($sql, $conn); 
print mysql_num_rows ($result);
if (mysql_num_rows ($result)>0) 
{ 
  $row = mysql_fetch_object ($result); 
  $image_type = $row->image_type; 
  $image = $row->image; 
  Header ("Content-type: $image_type"); 
  print $image; 
} 
?>
Also I tried the last change that you have asked me:"Replace index.php with image.php, as image.php file is responsible for displaying image, not the index.php file."i.e.,I used the below code but still the image is not getting displayed.
$str .= "<a href='image.php?iid=".$row["image_id"]."'>" 
           . $row["image_name"]."</a> "; 

Was This Post Helpful? 0
  • +
  • -

#12 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Not able fetch the image from mysql database and to display it.

Posted 14 January 2011 - 01:16 AM

Try this code-

index.php file
<?php
// index.php - by Hermawan Haryanto &lt;hermawan@codewalkers.com&gt;
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com

// database connection
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error());
@mysql_select_db ("dbname", $conn) OR DIE (mysql_error());

// Do this process if user has browse the 
// file and click the submit button
if ($_FILES) {
  $image_types = Array ("image/bmp",
                        "image/jpeg",
                        "image/pjpeg",
                        "image/gif",
                        "image/x-png");
  if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) {
    $userfile  = addslashes (fread 
                 (fopen ($_FILES["userfile"]["tmp_name"], "r"), 
                 filesize ($_FILES["userfile"]["tmp_name"])));
    $file_name = $_FILES["userfile"]["name"];
    $file_size = $_FILES["userfile"]["size"];
    $file_type = $_FILES["userfile"]["type"];

    if (in_array (strtolower ($file_type), $image_types)) {
      $sql = "INSERT INTO image "
             . "(image_type, image, image_size, image_name, image_date) ";
      $sql.= "VALUES (";
      $sql.= "'{$file_type}', '{$userfile}', '{$file_size}', "
             . "'{$file_name}', NOW())";
      @mysql_query ($sql, $conn);
      Header("Location:".$_SERVER["PHP_SELF"]);
      exit();
    }
  }
}

// Do this process of user has click 
// a file name to view or remove
if ($_GET) {
  $iid = $_GET["iid"];
  $act = $_GET["act"];
  switch ($act) {
    case 'rem':
      $sql = "DELETE FROM image WHERE image_id=$iid";
      @mysql_query ($sql, $conn);
      Header("Location:./index.php");
      exit();
      break;
    default:
      print "<img src=\"image.php?iid=$iid\">";
      break;
  }
}

?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File: 
<input type="file" name="userfile"  size="40">
<input type="submit" value="submit">
</form>
<?php
  $sql = "SELECT * FROM image ORDER BY image_date DESC";
  $result = mysql_query ($sql, $conn);
  if (mysql_num_rows($result)>0) {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      $i++;
      $str .= $i.". ";
      $str .= "<a href='image.php?iid=".$row["image_id"]."'>"
           . $row["image_name"]."</a> ";
      $str .= "[".$row["image_date"]."] ";
      $str .= "[".$row["image_size"]."] ";
      $str .= "[<a href='index.php?act=rem&iid=".$row["image_id"]
           . "'>Remove</a>]<br>";
    }
    print $str;
  }
?>
</body>
</html>



image.php file
<?php
// image.php - by Hermawan Haryanto &lt;hermawan@dmonster.com&gt;
// Example PHP Script, demonstrating Storing Image in Database
// Detailed Information can be found at http://www.codewalkers.com

// database connection
$conn = mysql_connect("localhost", "user", "password") 
  OR DIE (mysql_error());
@mysql_select_db ("dbname", $conn) OR DIE (mysql_error());
$sql    = "SELECT * FROM image WHERE image_id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
  $row = @mysql_fetch_array ($result);
  $image_type = $row["image_type"];
  $image = $row["image"];
  Header ("Content-type: $image_type");
  print $image;
}
?>



It should work, just change the database settings to match your own. Let me know if there is any further error occurs or not.
Was This Post Helpful? 1
  • +
  • -

#13 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 14 January 2011 - 03:43 AM

Thank you very much mere bhai(my brother). The above code is working successfully and I also found the reason for not getting output. Because I was using the below statement in the file "image.php" to check the value returned by the sql statement:
print mysql_num_rows ($result);
Keep your good work on. Meet you with another post. Khuda Hafees(Bye). :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#14 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Not able fetch the image from mysql database and to display it.

Posted 14 January 2011 - 03:48 AM

Thats great!

Happy to know its finally working & you are always welcome.
Was This Post Helpful? 0
  • +
  • -

#15 jagadish_c  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 31
  • Joined: 10-January 11

Re: Not able fetch the image from mysql database and to display it.

Posted 14 January 2011 - 03:53 AM

Atik, what do you think the problem was?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2