【问题标题】:Turning a db result set into a valid json with php使用 php 将 db 结果集转换为有效的 json
【发布时间】:2017-10-05 12:07:11
【问题描述】:

在我的 php 中,我正在运行一个简单的查询,该查询从我拥有的数据库中返回一个结果集(0 个或多个)。

目前在前面的结果看起来像这样:

name: Smoothie description: Banana Smothie name: Phad Thai description: Noodles with shrimps name: Noodles description: Noodles with noodles.

字符串也可以是这样的,又名name: Smoothie description: Banana Smothie 或具有更多条目,如上例所示。

下面的代码给了我这个:

[{"name":"Smoothie","description":"Banana Smothie"}][{"name":"Phad Thai","description":"Noodles with shrimps"}]

我想要的是,所以它可以只是一个 json 对象:

 [{"name":"Smoothie","description":"Banana Smothie"},{"name":"Phad Thai","description":"Noodles with shrimps"}]

这是我的 php:

    <?php
 include_once 'db/dbconnect.php';
 $input = json_decode(stripcslashes($_POST['data']));

 for ($i=0; $i < count($input); $i++) {
     $stmt=$con->prepare("SELECT recipes.recipeName, recipes.recipeDescription FROM ingredients, recipes, recipesingredients WHERE recipes.recipeId = recipesingredients.recipeIdFK AND recipesingredients.ingredientIdFK = ingredients.IngredientId AND ingredients.ingredientName = ?");
     $stmt->bind_param("s", $input[$i]);
     $stmt->execute();
     $stmt->store_result();
     $stmt->bind_result($db_recipe_name, $db_recipe_description);

     $rslt = array();
     $arr = 0;
     while ($stmt->fetch()) {
         $rslt[$arr] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
         $arr++;
     }
     $jsonRslt = json_encode($rslt);
     echo $jsonRslt;
 }
 ?>

有人可以帮我把它做成一个 json 对象吗?

【问题讨论】:

    标签: php arrays json associative-array


    【解决方案1】:

    在 for 循环中创建 php 数组。不要在循环内进行 json 编码/回显。仅在外部 for 循环外回显最终生成的 php 数组。另外,将响应标头设置为 json。 - 抱歉错字。手机回复。

    【讨论】:

      【解决方案2】:

      与其在循环中创建新的数组并在循环中进行 json_encode 和回显,不如在循环之前创建一个并将每个对象添加到其中。

      例子:

      <?php
      include_once 'db/dbconnect.php';
      $input = json_decode(stripcslashes($_POST['data']));
      
      // Create an array before the loop
      $json = [];
      
      for ($i=0; $i < count($input); $i++) {
          $stmt=$con->prepare("SELECT recipes.recipeName, recipes.recipeDescription FROM ingredients, recipes, recipesingredients WHERE recipes.recipeId = recipesingredients.recipeIdFK AND recipesingredients.ingredientIdFK = ingredients.IngredientId AND ingredients.ingredientName = ?");
          $stmt->bind_param("s", $input[$i]);
          $stmt->execute();
          $stmt->store_result();
          $stmt->bind_result($db_recipe_name, $db_recipe_description);
      
          while ($stmt->fetch()) {
              // Add each element to the main array
              $json[] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
          }
      }
      
      // json_encode and echo the main array
      echo json_encode($json);
      ?>
      

      【讨论】:

        【解决方案3】:

        for 循环内,$rslt 数组每次都会为每个i 重新初始化,这就是为什么您会得到多个 JSON 对象而不是单个对象的原因。

        您需要在for 循环之外初始化$rslt,并在for 循环之后将其编码为JSON。

        <?php
         include_once 'db/dbconnect.php';
         $input = json_decode(stripcslashes($_POST['data']));
        
         // Initialize array here
         $rslt = array();
         $arr = 0;
        
         for ($i=0; $i < count($input); $i++) {
             $stmt=$con->prepare("SELECT recipes.recipeName, recipes.recipeDescription FROM ingredients, recipes, recipesingredients WHERE recipes.recipeId = recipesingredients.recipeIdFK AND recipesingredients.ingredientIdFK = ingredients.IngredientId AND ingredients.ingredientName = ?");
             $stmt->bind_param("s", $input[$i]);
             $stmt->execute();
             $stmt->store_result();
             $stmt->bind_result($db_recipe_name, $db_recipe_description);
        
             while ($stmt->fetch()) {
                 $rslt[$arr] = array('name' => $db_recipe_name, 'description' => $db_recipe_description);
                 $arr++;
             }
         }
        
         // encode into JSON
         $jsonRslt = json_encode($rslt);
         echo $jsonRslt;
         ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-08
          • 1970-01-01
          • 2016-08-02
          • 2021-12-24
          • 1970-01-01
          • 1970-01-01
          • 2012-01-20
          • 2013-09-28
          相关资源
          最近更新 更多