2017-05-07 74 views
1

在我的php中,我正在運行一個簡單的查詢,它從我有的數據庫中返回一個結果集(0或很多)。將一個數據庫結果集轉換爲一個有效的json和php

目前在門前的rersult看起來是這樣的:

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對象嗎?

回答

1

除了在循環中創建json_encode和echo新的數組之外,還可以在循環之前創建一個數組,並將每個對象添加到循環中。

實施例:

<?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); 
?> 
+0

謝謝!有效 ! :) –

0

在for循環中創建php數組。不要json在循環內編碼/回顯。回聲只是最終產生的PHP數組外側for循環。另外,將響應頭設置爲json。 - 抱歉打字錯誤。從手機回答。

1

裏面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; 
?> 
相關問題