2017-05-07 181 views
0

在我的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." "; 
    } 
} 



?> 

有人可以帮我从查询结果与当前的代码,我有一个关联数组?

回答

1

只需将每个添加到数组。此外,使用现代JOIN语法:

<?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 i 
     JOIN recipesingredients ri 
      ON ri.ingredientIdFK = i.IngredientId 
     JOIN recipes r 
      ON r.recipeId = ri.recipeIdFK 
     WHERE i.ingredientName = ?"); 
    $stmt->bind_param("s", $input[$i]); 
    $stmt->execute(); 
    $stmt->store_result(); 
    $stmt->bind_result($db_recipe_name, $db_recipe_description); 

    $rslt = array(); 
    $rowno = 0; 
    while ($stmt->fetch()) { 
     $rslt[$rowno] = array('name' => $db_recipe_name, 'description' => $db_recipe_description); 
     $rowno++; 
     echo "name: ".$db_recipe_name." description: ".$db_recipe_description." "; 
    } 
    $jsonRslt = json_encode($rslt); 
    echo "<p>JSON Results:<pre>".$jsonRslt."</pre></p>\n"; 
    $stmt->close(); 
} 
+0

嘿。感谢你的回答。我尝试过了,使我这个输出: [“香蕉”,“米粉”] main.js:12
致命错误:调用一个成员函数bind_param()上布尔在/应用/ XAMPP /xamppfiles/htdocs/pm/search.php on line

+0

对不起,忘记了close()语句。如果这不能解决它,让我知道哪一行是29. –

0

刚上$语句调用使用fetchall(PDO :: FETCH_ASSOC)。它返回所有结果的关联数组。不需要使用while循环。

http://php.net/manual/en/pdostatement.fetchall.php#refsect1-pdostatement.fetchall-examples

include_once 'db/dbconnect.php'; 
$input = json_decode(stripcslashes($_POST['data'])); 
$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 = ?"); 

for ($i=0; $i < count($input); $i++) { 

    $stmt->bind_param("s", $input[$i]); 
    $stmt->execute(); 

    $result = $stmt->fetchAll(PDO::FETCH_ASSOC); 
    echo json_encode($result); 
} 
+0

感谢您的答案。我尝试了它并给了我这个输出:[“banana”,“rice noodles”] main.js:12
致命错误:调用成员函数bind_param()布尔型/Applications/XAMPP/xamppfiles/htdocs /pm/search.php on line

+0

@RobertRoss编辑了我的答案,你可以试试我添加的代码吗? – Gerjan

0

您可以使用fetch_assoc方法,并将其保存到一个数组。像

// In the beginning of your code initialize an ampty array 
$result = array(); 
// Have your query here. 
while($row = $stmt->fetch_assoc()) { 
    $result[] = $row; 
} 
$stmt->close(); 
echo json_encode($result); 
+0

嘿。感谢你的回答。我尝试了它并给了我这个输出:[“banana”,“rice noodles”] main.js:12
致命错误:调用成员函数bind_param()布尔型/Applications/XAMPP/xamppfiles/htdocs /pm/search.php on line