0

当通过 php 调用 SQL 查询时,我遇到了“允许的内存大小为......字节耗尽”的问题。我相信数组创建者不知何故陷入了循环。

我的PHP:

<?php
function confirm_query($result_set) {
    if (!$result_set) {
        die("DB query fail: " . mysql_error());
    }
}


$con = mysql_connect("mysql15.000webhost.com", "***_mw", "***");
if (!$con) {
        die("DB connect fail: " . mysql_error());
}

$mySqlDB = "a9414387_build";
$dbSelect = mysql_select_db($mySqlDB, $con);
if (!$dbSelect) {
    die("db select fail: " . mysql_error());
}


$sku = $_POST['serching'];
$query = "SELECT * FROM inventory1 WHERE id LIKE '{$sku}%'";
$result = mysql_query($query, $con);
$des = mysql_fetch_array($result);
$matches = mysql_num_rows($result);
$json = array();
if ($matches > 0) {
    while($des) {
        $bus = array(
            'description' => $des['description'],
            'price' => $des['price'],
            'sku' => $des['id'],
            'type' => $des['type'],
        );
        $jsonString = array_push($json, $bus);
    }

    echo json_encode($jsonString);

    } else {
        echo json_encode(NULL);
    }

die();
?>

但是,如果我注释掉:

while($des) {
            $bus = array(
                'description' => $des['description'],
                'price' => $des['price'],
                'sku' => $des['id'],
                'type' => $des['type'],
            );
            $jsonString = array_push($json, $bus);
        }

并用 $des 替换 $jasonString 我得到类似的东西:

{"0":"4040284","id":"4040284","1":"F","active":"F","2":"T","defaults":"T","3":"aurora,superNova","model":"aurora,superNova","4":null,"defmodel":null,"5":"mboard","type":"mboard","6":"105","price":"105","7":"Intel H67 (1155) Motherboard","description":"Intel H67 (1155) Motherboard","8":"1155","socket":"1155","9":"Not available at this time.","infoDiv":"Not available at this time.","10":"IntelH67Socket1155Motherboard.jpg","image":"IntelH67Socket1155Motherboard.jpg"}

我不打算扩展内存大小,b/c 一个小数组不应该这么大。

4

2 回答 2

2
$des = mysql_fetch_array($result);
$matches = mysql_num_rows($result);
$json = array();
if ($matches > 0) {
    while($des) {

$des您需要在循环内重新分配;否则,您将不断检查同一数据库行。尝试这样的事情:

$matches = mysql_num_rows($result);
$json = array();
if ($matches > 0) {
    while($des = mysql_fetch_array($result)) {
于 2012-10-01T15:25:26.733 回答
1

$des 被设置为数组的下一条记录,然后你在上面。这意味着它总是正确的,因为数组有内容,这会导致你想象的无限循环。$des = mysql_fetch_array($result)不会再次调用以继续下一个结果。

尝试删除第一个$des = mysql_fetch_array($result)并替换while($des)为:

while($des = mysql_fetch_array($result))
于 2012-10-01T15:25:07.837 回答