Json循环准备声明

时间:2016-01-11 09:25:46

标签: php json prepared-statement

如何从数据库中获取所有数据? 只有1个json数据:

  

{ “错误”:假​​, “用户”:{ “PRODUCT_ID”:42, “PRODUCT_NAME”:“鸡   W /无限   饭”, “PRODUCT_DESCRIPTION”: “”, “product_image”: “http://www.migrandegensan.com/image/1.jpg”}}

这是我的代码:

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;

    }
    }
}


?>

提前致谢!

1 个答案:

答案 0 :(得分:0)

请尝试以下代码: 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;
   }
}
?>