dream8877 2012-07-18 15:37
浏览 34
已采纳

在PHP中使用array_push

I apologize a head of time for the lengthy/busy question. I am trying to take results from a MySQL query and base64_encode the Image blob data, return it to the array and then finally json_encode the results so I can use them in my Android application. I know everything on the Android side is set up properly.

What I have is the following:

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);

Returns 8 results:

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

What I need:

I want the results to appear like this, with only 4 results.

[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 Application snippet:

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

Android Error:

07-18 11:28:52.573: E/onPostExecute(14562): FAILED: No value for img
  • 写回答

3条回答 默认 最新

  • dongmei9508 2012-07-18 15:40
    关注

    Try this:

    array_push($rows, array_merge($businessName, $finalImg));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么