2017-05-07 87 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