0

对于这个冗长/忙碌的问题,我很抱歉。我正在尝试从 MySQL 查询和base64_encode图像 blob 数据中获取结果,将其返回到数组,然后最后返回json_encode结果,以便我可以在我的 Android 应用程序中使用它们。我知道 Android 端的所有内容都已正确设置。

我所拥有的是以下内容:

PHP/SQL:

$query = "SELECT `locations`.`businessName`, `photos`.`img` 
        FROM `locations` 
        JOIN `photos` ON `locations`.`co_id` = `photos`.`co_id` 
        WHERE `locations`.`businessName` = '".$companyID."'";

    mysql_connect($dbserver, $dbusername, $dbpassword) or die(mysql_error());
    mysql_select_db($dbname) or die(mysql_error());

    $result = mysql_query($query) or die(mysql_error());  
    $num = mysql_numrows($result);
    $row = mysql_fetch_assoc($result);

    $i = 0;
    $rows = array();
    while ($i < $num) {

        $img = mysql_result($result, $i, "img");
        $finalImg['img'] = base64_encode($img);
        $businessName['businessName'] = mysql_result($result, $i, "businessName");

        $finalArray = array_push($rows, $businessName, $finalImg);
        // I know that array_push is pushing each variable as a separate array item
        // I tried creating an alternative variable that amends the two together
        // But that didn't work, result printed [Array, Array] [Array, Array]
        // Was I on the right track?

        $i++;
    }

    print json_encode($rows);

返回 8 个结果:

[0] => {
    ["businessName"]=> string(12) "Some Company" }
[1] => {
    ["img"]=> string(145968) "/9j/4AAQSkZJRgABAQEAYABgAAD/4QIw..." }

我需要的:

我希望结果看起来像这样,只有 4 个结果。

[0] => {
    ["businessName"] => string(12) "Some Company" 
    ["img"] => string(145968) "/9j/4AAQSkZJRgABAQEAYABgAAD/4QIw..." }
[1] => {
    ["businessName"] => string(12) "Some Company", 
    ["img"] => string(145968) "/9j/4AAQSkZJRgABAQEAYABgAAD/4QIw..." }

Android 应用程序片段

jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
    JSONObject jObject = jArray.getJSONObject(i);
    String testerPhoto = jObject.getString("img");
    //Process image. Base64 decode... etc

安卓错误:

07-18 11:28:52.573: E/onPostExecute(14562): FAILED: No value for img
4

3 回答 3

3

这要简单得多:

$i = 0;
$rows = array();
while ($i++ < $num) {
    $img = mysql_result($result, $i, "img");
    $rows[] array(
        'businessName' => mysql_result($result, $i, "businessName"),
        'img' => base64_encode($img),
    );
}
于 2012-07-18T15:41:42.307 回答
1

试试这个:

array_push($rows, array_merge($businessName, $finalImg));
于 2012-07-18T15:40:23.417 回答
1

那这个呢?

$rows[] = array(
  'img' => base64_encode($img),
  'businessName' => mysql_result($result, $i, "businessName")
);

或者

array_push($rows, array_merge($businessName, $finalImg));
于 2012-07-18T15:44:54.953 回答