来自MySQL数据库的空PHP输出,用于longblob

时间:2019-04-11 01:16:45

标签: php mysql blob

我有一个Android应用程序,该应用程序拍摄照片,将位图转换为Base64,然后将Base64字符串提交到MySQL数据库(通过PHP)以存储为longblob。这部分效果很好!实际上,我可以从phpMyAdmin下载longblob作为完美的Base64字符串,并轻松转换为JPEG照片。

问题是我的用于获取Blob的PHP代码返回一个空字符串:

{
    "owner":"Unknown",
    "pet_name":"Unknown",
    "last_seen":"2019-04-09 11:17:19",
    "contact":"999-888-7654",
    "description":"rubber ducky, lotsa fun",
    ***"photo":""***,
    "location":"Some location"
}

PHP获取器:

function getReports() {
    $stmt = $this->con->prepare("SELECT owner, pet_name, last_seen, contact, description, photo, location FROM Pets");
    $stmt->execute();
    $stmt->bind_result($owner, $pet_name, $last_seen, $contact, $description, $photo, $location);

    $reports = array();

    while($stmt->fetch()) {
        $report  = array();
        $report['owner'] = $owner;
        $report['pet_name'] = $pet_name;
        $report['last_seen'] = $last_seen;
        $report['contact'] = $contact;
        $report['description'] = $description;
        $report['photo'] = $photo;
        $report['location'] = $location;

        array_push($reports, $report);
    }

    return $reports;
}

一个有趣的旁注,如果我使用下面的代码代替上面的代码,但我确实获得了完整的Base64字符串,但是在整个过程中都添加了转义()和换行符(\ n):

//Select everything from table
$sql= "SELECT * FROM Pets";

//Confirm results
if($result = mysqli_query($con, $sql)) {
    //Results? Create array for results and array for data
    $resultArray = array();
    $tempArray = array();

    //Loop through results
    while($row=$result->fetch_object()) {
        // Add each result in results array
        $tempArray=$row;
        array_push($resultArray,$tempArray);
    }

    //Encode array to JSON and output results
    echo json_encode($resultArray);
}

我想找到一种方法来修复上述PHP代码。我在想我的字符串对于$photo值来说太长了吗?任何建议将不胜感激。

更新:从Selecting Blob from MYSQL, getting null起,我确实设法将Base64而不是空字符串输出到了。但是,我仍然遇到转义符和换行符的问题。

这里有帮助吗?

1 个答案:

答案 0 :(得分:0)

我设法通过转换为原始函数来输出Base64行:

    $stmt = $this->con->prepare("SELECT owner, pet_name, last_seen, contact, description, CAST(photo as CHAR(1000000) CHARACTER SET utf8) as photo, location FROM Pets");

尽管这允许接收Base64字符串,但仍包含不需要的字符。 不需要的字符是由JSON_ENCODE引起的。下面是我用来修复它的内容。

基本上,1.删除添加的字符,然后2.告诉JSON_ENCODE不要使用JSON_UNESCAPED_SLASHES打印转义字符。

对于功能getReports()

function getReports() {
    $stmt = $this->con->prepare("SELECT owner, pet_name, last_seen, contact, description, CAST(photo as CHAR(1000000) CHARACTER SET utf8) as photo, location FROM Pets");
    $stmt->execute();
    $stmt->bind_result($owner, $pet_name, $last_seen, $contact, $description, $photo, $location);

    $reports = array();

    while($stmt->fetch()) {
        $report  = array();
        $report['owner'] = $owner;
        $report['pet_name'] = $pet_name;
        $report['last_seen'] = $last_seen;
        $report['contact'] = $contact;
        $report['description'] = $description;
        $photo = str_replace("\n","",$photo);
        $photo = str_replace("\\/","/", $photo);
        $photo = stripcslashes($photo);
        $report['photo'] = $photo;
        $report['location'] = $location;

        array_push($reports, $report);
    }

    return $reports;
}

在Api脚本中,将返回值更改为

echo json_encode($resultArray);

echo json_encode($resultArray, JSON_UNESCAPED_SLASHES);

现在一切正常。这是最佳做法吗?我不确定。我确定存储base64可能不是...