Hi frnds,


I want to store images in database and retrieve. I can successfully stored. But i dont know how retrieve it. If any one know pls help me. I paste my code here.

fimg.php

<body>
<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
</body>

insert.php

<?php
$username = "root";
$password = "";
$host = "localhost";
$database = "simg";

$link = mysql_connect($host, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}


mysql_select_db ($database);  
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      
      $tmpName  = $_FILES['image']['tmp_name'];  
       
     
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);
      



      $query = "INSERT INTO img";
      $query .= "(image) VALUES ('$data')";
      $results = mysql_query($query, $link);
      

      print "Thank you, your file has been uploaded.";
      
}
else {
   print "No image selected/uploaded";
}


mysql_close($link);

?>

seimg.php

<form action="seimg.php">
Enter image id:<input type="text" name="field" />
<input type="submit" name="submit" value="show" />

<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "simg";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$b =$_REQUEST['field'];

$query = mysql_query("SELECT * FROM img WHERE id='$b'");

$row = mysql_fetch_array($query);
$content = $row['image'];

?>

Recommended Answers

All 22 Replies

$query = mysql_query("SELECT * FROM img WHERE id='$b'");

$row = mysql_fetch_array($query);
echo "<img src='".$content = $row['image']."' />";

hi frnd,

I use that link and do the same way. but i dont know how link one file to another in this program . successfully image downloaded, but not stored into database. can you help me?

Hi..

1.
$query = mysql_query("SELECT * FROM img WHERE id='$b'");
2.

3.
$row = mysql_fetch_array($query);
4.
echo "<img src='".$content = $row."' />";

this coding working.But it not displayed image. It displayed some binary data. wat error in this ?

Hi frnds,

I used $b =$_REQUEST; It shows error in this line."Notice: Undefined index: field in C:\wamp\www\base\seimg.php on line 24" .

Member Avatar
echo "<img src='". <?php echo $row['image']?>."' />";

do you stored the path of the image file in the database? if yes then you can use the above method.

Use $_POST or $_GET to the retrieve field.

I used this. But it shows an error."Parse error: parse error in C:\wamp\www\base\seimg.php on line 30"

line 30:echo "<img src='". <?php echo $row?>."' />";

echo "<img src='". <?php echo $row['image']?>."' />";

lol of course its a parse error, i assume your inside a php tag coz you use echo, you cant nest another php inside php, instead you do it like this

echo "<img src='".$row['image']."' />";

REMEMBER

if you the code is inside the a php tag, use

echo "<img src='".row['image']."' />";

if not

<img src='<?=$row['image']?>' />

Hi frnds,

I did same wat u said. but it shows an error. I dont know. if u can pls say to me. I paste my code here.

seimg.php

<body>

<form action="seimg.php">
Enter image id:<input type="text" name="field" />
<input type="submit" name="submit" value="show" />

<?php

$username = "root";
$password = "";
$host = "localhost";
$database = "simg";

@mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

@mysql_select_db($database) or die("Can not select the database: ".mysql_error());

$b =$_GET['field'];

$query = mysql_query("SELECT image FROM img WHERE id='$b'");

 $row = mysql_fetch_array($query);
 
 
 
echo "<img src='".$row['image']."' />";

   
      



?>  
   
  
</form>

it shows "Notice: Undefined index: field in C:\wamp\www\base\seimg.php on line 24" this error.

theres nothing wrong in the echo, mind if i ask? where did you get $b =$_GET, and where actually the error is pointing?

actually error pointing

$b =$_GET; this line only. i dont know wat error in this line. Any one know say to me.

Member Avatar

You have to use $_GET if you mentioed the method as GET or otherwise use $_POST

Hi frnd,

really thanks for reply. But again it shows an error. Can you help me for this?

Member Avatar

What was the error?

the same error:
"Notice: Undefined index: field in C:\wamp\www\base\seimg.php on line 24"

you dont know what your doing bro... check you form if your using get or post

Please use link method to save your pics. and just save reference of that link in your DB. It will improve efficiency

use the following to get the photo from where ur storing them. In this case i assume that the image is stored at the follder Photoz

<img src="photoz/<?php echo $imagename; ?>" width="200" height="300"alt="<?php echo $name; ?>"/>
Member Avatar

I hope you missed the data somewhere, check your data with

Sample:

//check that $_POST['foo'] exists 
if (isset($_POST['foo'])) { 
    //$_POST['foo'] exists 
    $foo = $_POST['foo']; //we might perform input validation here 
} else { 
    //$_POST['foo'] does not exist 
    $foo = ''; //assign a default value 
} 

//use $foo 
echo $foo;

also check with empty($_POST). replace your variable instead of foo.

Hi frnd,

I used this coding. but it didnt display the value. now i paste my code and output here. Otherwise if you can give full coding for retrieving image from database.

seimg.php

<body>

    <form action="seimg.php">
    Enter image id:<input type="text" name="foo" />
    <input type="submit" name="submit" value="show" />

    <?php

    $username = "root";
    $password = "";
    $host = "localhost";
    $database = "simg";

    @mysql_connect($host, $username, $password) or die("Can not connect to database: ".mysql_error());

    @mysql_select_db($database) or die("Can not select the database: ".mysql_error());



      //check that $_POST['foo'] exists

      if (isset($_POST['foo'])) {

      //$_POST['foo'] exists

      $foo = $_POST['foo']; //we might perform input validation here

      } else {

      //$_POST['foo'] does not exist

     $foo = ''; //assign a default value

      }


      //use $foo

      echo $foo;


    $query = mysql_query("SELECT * FROM img WHERE id='".$foo."'");
$row = mysql_fetch_array($query);
$content = $row['image'];

header('Content-type: image/jpg');
echo $content;


    ?>

       </form>


</body>
</html>

Output:

[url]http://localhost/base/seimg.php?foo=1&submit=show[/url]

I strongly recommend against storing images in a database store the file name with extension ie picture.gif and save the actual picture to a directory on the server I would resize it first using php, then it is easy to call. Saved images in a database it takes up alot of space also writing the script to read them from a database is really hard.

Hi frnd,

Actually i write program for storing image path in database and retrieve the path then display image. if you know the code pls send me.

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.