0

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'];

?>

Edited by nav33n: Use [code] [/code] tags to wrap your code for easy readability.

7
Contributors
22
Replies
31
Views
7 Years
Discussion Span
Last Post by sugikrish
0
$query = mysql_query("SELECT * FROM img WHERE id='$b'");

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

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?

0

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 ?

0

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" .

0
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.

0

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?>."' />";

0
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']?>' />

Edited by vaultdweller123: n/a

0

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.

Edited by nav33n: Use [code] [/code] tags.

0

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

Edited by vaultdweller123: n/a

0

actually error pointing

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

0

Hi frnd,

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

0

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

0

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

0

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; ?>"/>
0

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.

0

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] 

Edited by mike_2000_17: Fixed formatting

0

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.

Edited by shadiadiph: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.