php - 将 public_html/image 中的图像插入 mysql

标签 php mysql blob

无法将图像从 public_html/image 插入带有 BLOB 的 mysql 表

我设置了表:

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

尝试将图像插入mysql表

$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();

已成功将“香蕉”保存到项目列,但图像列为空。 我希望 BLOB 存储到图像列。

我做错了什么?

最佳答案

如果您的图像位于远程服务器中,您将尝试此操作。

$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);

如果您的图像文件位于您的服务器中,您可以使用 LOADFILE mysql的功能

关于php - 将 public_html/image 中的图像插入 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879923/

相关文章:

javascript - 如何使用 HTML5 重命名下载的文件?

MySQL select join where AND where

mysql - SQL。我需要知道某个客户是否有超过 3 个并发优惠

sql - 更改表 - 为 COLUMN 类型 BLOB 添加默认值

php - 同步: how get a blob from MySql and save it in another MySql with php

php - Magento 教程为后端创建模块(管理页面)

PHP:简单的 PDO 循环不起作用?

javascript - Symfony 2 - 根据另一个字段中的选择动态隐藏字段

Mysql #1235 错误代码,当创建 2 个具有不同目的的触发器时

javascript - 在 IE9 中使用 JS 将 PDF 下载为 Blob