在我的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对象吗?
谢谢!有效 ! :) –