将一个数据库结果集转换为一个有效的json和php
问题描述:
在我的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对象吗?
答
除了在循环中创建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);
?>
答
在for循环中创建php数组。不要json在循环内编码/回显。回声只是最终产生的PHP数组外侧for循环。另外,将响应头设置为json。 - 抱歉打字错误。从手机回答。
答
里面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;
?>
谢谢!有效 ! :) –