无法将图像从 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/