有人能修复这个代码吗?我想使用php-json从数据库加载所有数据。。。但是我怎样才能做到呢?
<?php require_once 'include/db_functions.php'; $db = new DB_Functions();
$user = $db->getID();
$response["products"] = array();
if($user){
while ($user) {
$productstbl = array();
$productstb["product_name"] = $user["product_name"];
array_push($response["products"], $user);
}
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in updating!";
echo json_encode($response);
}
?>
提前感谢
在您的代码中是否有将$user
变量设置为FALSE
的方法?
因为在我看来,你有一个无限循环:
while ($user){
....
....
//You shoul have a way to set $user to false since it is your flag
$user = false;
}
Sample.php
<?php require_once 'include/db_functions.php'; $db = new DB_Functions();
$user = $db->getID();
$response["products"] = array();
if($user){
while ($user) {
$productstbl = array();
$productstb["product_name"] = $user["product_name"];
array_push($response["products"], $user);
}
echo json_encode($response);
} else {
// user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Unknown error occurred in updating!";
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 product_name FROM products WHERE pro_cat_id = ?");
$stmt->bind_param("s", $pro_cat_id);
$stmt->execute();
$stmt->num_rows();
while($user=$stmt->fetch()) {
return $user;
}
$stmt->close();
}
}?>