在我的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對象嗎?
謝謝!有效 ! :) –