【问题标题】:Returning blob data from MySQL in PHP and converting to base64_encode to add to an array在 PHP 中从 MySQL 返回 blob 数据并转换为 base64_encode 以添加到数组
【发布时间】:2016-08-03 15:54:36
【问题描述】:

我正在尝试从 MySQL 中的表中检索所有数据并将其添加到数组中。

我想将 blob 转换为编码字符串并将其添加到包含其他两列的数组中,然后将其作为 JSON 字符串返回。请参阅下面的我尝试的代码:

<?php 
//Importing Database Script 
require_once('dbConnect.php');

//Creating sql query
$sql = "SELECT * FROM picture";

//getting result 
$r = mysqli_query($con,$sql);

//creating a blank array 
$result = array();

//looping through all the records fetched
while($row = mysqli_fetch_array($r)){

    //Pushing name and id in the blank array created 
    array_push($result,array(
        "pictureID"=>$row['pictureID'],
        "listingID"=>$row['listingID'],
        "listingImage"=>$row [base64_encode('listingImage')]
    ));
}

//Displaying the array in json format 
echo json_encode(array('result'=>$result));

mysqli_close($con);

代码当前返回的 blob 图像为空值。

【问题讨论】:

  • 试试"listingImage"=&gt;base64_encode($row['listingImage'])
  • 当然,如果您按照应有的方式将文件保存在文件系统上,那么所有这些混乱都是可以避免的。

标签: php mysql arrays base64 blob


【解决方案1】:

尝试换行:

"listingImage"=>$row [base64_encode('listingImage')]

"listingImage"=> base64_encode($row["listingImage"]);

【讨论】:

    猜你喜欢
    • 2011-05-06
    • 2013-01-11
    • 1970-01-01
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 2013-10-03
    • 2014-10-21
    相关资源
    最近更新 更多