如何返回多个/所有行从mysql数据库通过php和json


How to return multiple/all rows from mysql db via php and make json?

我在我的php文件中有这个:

$response = array();
    $user = $db->test($email);
                if ($user) {
                    // user stored successfully
                    $response["success"] = 1;
                    $response["user"]["email"] = $user["email"];
                    $response["user"]["imagepath"] = $user["imagepath"];
                    $response["user"]["about"] = $user["about"];
                    echo json_encode($response);
                } else {
                    // user failed to store
                    $response["error"] = 1;
                    $response["error_msg"] = "JSON Error occured in db";
                    echo json_encode($response);
                }

测试函数为

public function test($email){
            $result = mysql_query("SELECT * FROM activities WHERE email = '$email'");
            // return user details
            return mysql_fetch_array($result);
    }

测试函数返回多行,但是php只发送json,其中包含与该电子邮件匹配的数据库中的第一行。我怎么能做一个foreach()或类似的东西来编码测试函数返回的一切?

首先你必须拒绝使用mysql_fetch_array()函数(读这个)。

接下来,您必须理解变量'user'包含所有找到的用户的列表(公平地说,它应该被称为'users')

$response = array();
$users = $db->test($email);
foreach($users as $user){
    if ($user) {
        // user stored successfully
        $response["success"] = 1;   
        $response["users"][] = array(
            "email" => $user["email"],
            "imagepath" => $user["imagepath"],
            "about" => $user["about"]
        );
    } else {
        if( empty($response) ){
            // user failed to store
            $response["error"] = 1;
            $response["error_msg"] = "JSON Error occured in db";
        }
    }
}
echo json_encode($response);

和更新测试功能:

public function test($email){
    $result = mysql_query("SELECT * FROM activities WHERE email = '$email'");
    // get all users details
    $users = array();
    while($user = mysql_fetch_array($result)){
        $users[] = $user;
    }
    return $users;
}

您需要遍历用户数组。您还需要增加数组键,以便下一个条目不会覆盖前一个条目。

<?php
$response = array();
$user = $db->test($email);
if ($user) {
  // user stored successfully
  $response['success'] = 1;
  $key = 0; // unique key
  // Loop through user array
  foreach($user as $row) {
    $response[$key] = array(
      'email' => $row["email"],
      'imagepath' => $row["imagepath"],
      'about' => $row["about"];
    );
    $key++; // increment key
  }
} else {
  // user failed to store
  $response['error'] = 1;
  $response['error_msg'] = "JSON Error occurred in db";
}
var_dump($response); // debugging
$json = json_encode($response);
echo($json);
?>

json_encode()之前的响应数组:

array(3) {
  ["success"]=> string(1) "1"
  [0]=>
  array(3) {
    ["email"]=> string(21) "someemail@address.com"
    ["imagepath"]=> string(13) "path/to/image"
    ["about"]=> string(26) "about content etc etc etc."
  }
  [1]=>
  array(3) {
    ["email"]=> string(22) "someemail2@address.com"
    ["imagepath"]=> string(14) "path/to/image2"
    ["about"]=> string(26) "about content etc etc etc."
  }
}

json_encode()后的响应:

string(227) "{"success":"1","0":{"email":"someemail@address.com","imagepath":"path'/to'/image","about":"about content etc etc etc."},"1":{"email":"someemail2@address.com","imagepath":"path'/to'/image2","about":"about content etc etc etc."}}"