2012-07-18 48 views
0

對於長時間/繁忙問題,我表示歉意。我試圖從MySQL查詢和Image Blob數據中獲取結果,將其返回到數組,然後最終json_encode結果,以便我可以在我的Android應用程序中使用它們。我知道Android方面的一切都設置正確。在PHP中使用array_push

我有如下:

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 

Android的錯誤:

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

請不要使用新代碼'mysql_ *'功能。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 – 2012-07-18 15:41:17

+0

我不知道,不需要在上述語法的手冊上查看一下,因此沒有紅框意識。我會糾正自己。感謝您的參考。 – jnthnjns 2012-07-18 15:45:10

回答

1

試試這個:

array_push($rows, array_merge($businessName, $finalImg)); 
+0

我知道它必須是我沒有想到的東西。謝謝。完美工作。 – jnthnjns 2012-07-18 15:41:45

+0

沒問題,你真正需要的是陣列數組;) – Zbigniew 2012-07-18 15:42:34

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), 
    ); 
} 
1

這個是什麼?

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

或者

array_push($rows, array_merge($businessName, $finalImg));