1

I've create a table where I've saved images through "LongBLOB". I need to show those images. i can save images in my sql but when i want to read and display them i have a problem, "can not be displayed because it contains errores"

im usi8ng this code to save the image to my sql table

<?php
$username = "root";
$password = "";
$host = "localhost";
$database = "imgtest";
$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);
$namee = $_FILES['image']['name'];
mysql_query("INSERT INTO image(cap,image) VALUES ('$namee','$data')");
}
    ?>

and im using this code to read and display the image from my sql table

 <?php
$con=mysqli_connect("localhost","root","","imgtest");
$wich=$_POST['wich'];
$resoult=mysqli_query($con,"SELECT * FROM image WHERE id LIKE '$wich'");
$imgd = $_GET['img'];
$row = mysqli_fetch_array($resoult);
$image = $row['image'];
header("content-type:image/jpeg");
echo $image;

?>

anyone can help me with this??

  • possible duplicate of [How can I store and retrieve images from a MySQL database using PHP?](http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php) – pstenstrm Jun 08 '14 at 08:14
  • This is a mix of old code, using a deprecated API, and the modern method. Strange. Also, change your password) – Strawberry Jun 08 '14 at 08:23
  • $pstenstrm i used that code but still the same problem – user3719210 Jun 08 '14 at 08:59

2 Answers2

0

It is strongly advised to store images within your file structure (a directory on your server, cloud server, or other accessible location) and only keep a reference such as a url, file name or path path in the database in order to recreate a link or path to the actual image. It is a bad practice to keep images in a database.

Silvertiger
  • 1,680
  • 2
  • 19
  • 32
0
  1. Why are you using mysql_ library - it is deprecated.
  2. You need to store binary data. Use prepared statements. In that way you get back binary data.
  3. From 2 you can dump addslashes. That is the root of the problem
Ed Heal
  • 59,252
  • 17
  • 87
  • 127