简体   繁体   中英

Turning a query result to an associative array

In my php I am running a simple query which returns a resultset(0 or many) from the database I have.

Currently on the fronted the rersult looks like this :

name: Smoothie description: Banana Smothie name: Phad Thai description: Noodles with shrimps name: Noodles description: Noodles with noodles.

The string can also look like this, aka name: Smoothie description: Banana Smothie or with more entries, like in the example above.

What I am aiming to have is an associative array from my result, which I can turn into json string and pass it to the frontend.

Unfortunately what i tried so far didn't work.

This is my 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." ";
  }
}



 ?>

Can someone help me make the result from the query to an associative array with the current code i have?

Just add each one to an array. Also, use modern JOIN syntax:

<?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();
}

Just call fetchAll(PDO::FETCH_ASSOC) on $stmt. It returns a associative array of all results. No need to use a while loop.

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);
}

You can use fetch_assoc method and save it into an array. Like

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM