JSON-第14行的/home/migrande/public_html/sample.php中允许的内存大小为134217728字节(尝试分配72字节)
有人能修复这个代码吗?我想使用php json加载数据库中的所有数据。。。但我怎样才能做到呢JSON-第14行的/home/migrande/public_html/sample.php中允许的内存大小为134217728字节(尝试分配72字节),php,json,Php,Json,有人能修复这个代码吗?我想使用php json加载数据库中的所有数据。。。但我怎样才能做到呢 <?php require_once 'include/db_functions.php'; $db = new DB_Functions(); $user = $db->getID(); $response["products"] = array(); if($user){ while ($user) {
<?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();
}
}?>
[Allowed memory size]错误,大多发生在存在无限循环时。您知道如何修复此问题吗?这是完整的代码吗?您在“while($user){…}”处有一个无限循环-至少我看不到$user在哪里再次设置为false。我可以在哪里设置$user=false?