0

Cannot INSERT image from public_html/image to mysql table with BLOB

I set up table:

CREATE TABLE `db`.`fruit` ( `item` VARCHAR(40) NOT NULL , `image` BLOB NOT NULL ) ENGINE = InnoDB;

Try to insert image to mysql table

$item = 'banana';
$image = 'http://www.somesite.org/image/banana.jpg';

$con = new mysqli('localhost','','','db');
$sql = $con->prepare("INSERT INTO fruit (item, image) VALUES (?,?)");
$sql->bind_param("sb", $item, $image);
$sql->execute();

Managed to save 'banana' to the item column, but image column is empty. I expect BLOB to be store to image column.

What have I done wrong??

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Newbee123
  • 61
  • 6
  • [Sure you want to save images in the database?](https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) – Progman Dec 21 '18 at 10:37

1 Answers1

0

If your image is located in the remote server, you will try this.

$image_data = file_get_content($image_url);
$sql = $con->prepare("INSERT INTO fruit (item, image) VALUES (:item,:image)");
$stmt->bind_param("item", $item);
$stmt->bind_param("image", $image_data);

And if your image file is located in your server, you can use LOADFILE function of mysql

Star_Man
  • 1,091
  • 1
  • 13
  • 30