我为冗长/繁忙的问题道歉。我试图从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
Android错误:
07-18 11:28:52.573: E/onPostExecute(14562): FAILED: No value for img
答案 0 :(得分: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 :(得分:1)
试试这个:
array_push($rows, array_merge($businessName, $finalImg));
答案 2 :(得分:1)
这个怎么样?
$rows[] = array(
'img' => base64_encode($img),
'businessName' => mysql_result($result, $i, "businessName")
);
或者
array_push($rows, array_merge($businessName, $finalImg));