在我的php中,我正在运行一个简单的查询,它从我拥有的数据库中返回一个结果集(0或多个)。将查询结果转换为关联数组
目前在门前的rersult看起来是这样的:
name: Smoothie description: Banana Smothie name: Phad Thai description: Noodles with shrimps name: Noodles description: Noodles with noodles.
的字符串也可以是这样的,又名name: Smoothie description: Banana Smothie
或多个条目,就像上面的例子。
我的目标是从我的结果,我可以变成json字符串,并将其传递到前端的关联数组。
不幸的是,我到目前为止尝试没有工作。
这是我的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);
while ($stmt->fetch()) {
echo "name: ".$db_recipe_name." description: ".$db_recipe_description." ";
}
}
?>
有人可以帮我从查询结果与当前的代码,我有一个关联数组?
嘿。感谢你的回答。我尝试过了,使我这个输出: [“香蕉”,“米粉”] main.js:12
致命错误:调用一个成员函数bind_param()上布尔在/应用/ XAMPP /xamppfiles/htdocs/pm/search.php on line
–
对不起,忘记了close()语句。如果这不能解决它,让我知道哪一行是29. –