简体   繁体   中英

Json loop prepared statement

How I can make get the all the data from database? Only 1 json data:

{"error":false,"user":{"product_id":42,"product_name":"Chicken w/Unlimited Rice","product_description":"","product_image":"http://www.migrandegensan.com/image/1.jpg"}}

This is my code:

Chicken.php

<?php

require_once 'include/db_functions.php';
$db = new DB_Functions();

$response = array("error" => FALSE);

//select data from chicken 
$user = $db->getID();
        if ($user) {

            $response["error"] = FALSE;
            $response["user"]["product_id"]=$user["product_id"];
            $response["user"]["product_name"] = $user["product_name"];
            $response["user"]["product_description"] = $user["product_description"];
            $response["user"]["product_image"] = $user["product_image"];


            echo json_encode($response);
        } else {
            // user failed to store
            $response["error"] = TRUE;
            $response["error_msg"] = "Unknown error occurred in registration!";
            echo json_encode($response);
        }

?>

db_functions.php

<?php

class DB_Functions {

    private $conn;

    // constructor
    function __construct() {
        require_once 'db_connect.php';
        // connecting to database
        $db = new Db_Connect();
        $this->conn = $db->connect();
    }

    // destructor
    function __destruct() {

    }
public function getID() {
        $pro_cat_id = "101";

            $stmt = $this->conn->prepare("SELECT * FROM products WHERE pro_cat_id = ?");
            $stmt->bind_param("s", $pro_cat_id);
            $stmt->execute();
            $user = $stmt->get_result()->fetch_assoc();
            $stmt->close();

            while ($user) {
            return $user;

    }
    }
}


?>

Thanks in Advance!

Please try below code : Chicken.php

require_once 'include/db_functions.php';
$db = new DB_Functions();

$response = array("error" => FALSE);

//select data from chicken 
$user = $db->getID();
   if ($user) {
      $response["error"] = FALSE;
      foreach ($user as $key => $value) {
         # code...
         $response["user"]["product_id"]=$value["product_id"];
         $response["user"]["product_name"] = $value["product_name"];
         $response["user"]["product_description"] = $value["product_description"];
         $response["user"]["product_image"] = $value["product_image"];
      }
      echo json_encode($response);
   } else {
      // user failed to store
      $response["error"] = TRUE;
      $response["error_msg"] = "Unknown error occurred in registration!";
      echo json_encode($response);
   }

?>

db_functions.php

<?php

class DB_Functions {

 private $conn;

 // constructor
 function __construct() {
     require_once 'db_connect.php';
     // connecting to database
     $db = new Db_Connect();
     $this->conn = $db->connect();
 }

 // destructor
 function __destruct() {

 }

public function getID() {
   $pro_cat_id = "101";

      $stmt = $this->conn->prepare("SELECT * FROM products WHERE pro_cat_id = ?");
      $stmt->bind_param("s", $pro_cat_id);
      $stmt->execute();
      $user = $stmt->get_result()->fetch_assoc();
      $stmt->close();

      return $user;
   }
}
?>

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