繁体   English   中英

Json循环准备语句

[英]Json loop prepared statement

我怎样才能从数据库中获取所有数据? 仅1个json数据:

{“错误”:false,“用户”:{“ product_id”:42,“ product_name”:“带无限米饭的鸡肉”,“ 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;

    }
    }
}


?>

提前致谢!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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