【问题标题】:Select blob/image from database returns NULL从数据库中选择 blob/图像返回 NULL
【发布时间】:2014-06-29 01:40:53
【问题描述】:

这是我如何将图像推送到数据库的代码:

$title = $_REQUEST['title'];
$description = $_REQUEST['description'];
$image = $_REQUEST['image'];
$bdata = addslashes($image);

$query = "insert into Images (title, description, image) values ($title, $description, '$bdata')" or die("Error in the consult.." . mysqli_error($connection));

$result = $connection->query($query);
$connection->close();

我再次检查了数据库,它存储成功。

这是从表中获取数据的代码:

$query = "select * from Images" or die("Error in the consult.." . mysqli_error($connection));
$result = $connection->query($query);
$events = array();

while ($event = $result->fetch_array(MYSQLI_ASSOC)) {       

    $events[] = $event;
}

header('Content-type: application/json');

echo json_encode($events);

$result->free();
$connection->close();

结果我收到下一个 json:

{
id: "34",
title: "some image",
description: "some description",
image: null
}

如您所见,“图像”字段为空。在phpAdmin中我看到有一个数据。

【问题讨论】:

  • 不要使用“SELECT *”。命名您实际想要返回的列。 (否则我们没有机会)

标签: php json blob


【解决方案1】:

首先:使用prepared statements 摆脱烦人的SQL injection。


JSON 不支持二进制数据:

值可以是双引号中的字符串、数字、真假或空值、对象或数组。

使用 Base64、BSON、Smile 或 UBJSON 之类的名称。另见:Binary Data in JSON String. Something better than Base64

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2021-05-19
    • 2019-05-21
    • 2021-12-25
    相关资源
    最近更新 更多