mysqli_prepared_query:允许的内存大小为134217728字节

时间:2016-11-17 09:07:42

标签: mysqli prepared-statement

我在stackoverflow或google中看到类似的问题,但没有任何帮助我。 而且我不想将memory_limit设置为-1,我的意图是在我的代码中真正解决问题。

我的代码:

    function mysqli_prepared_query($link, $sql, $types = false, $params = false) {

    $multiQuery = false;

    if ($stmt = mysqli_prepare($link, $sql)) {
        if (!is_array($params))
            $params = array(array($params));
        elseif (count($params) == count($params,1))
            $params = array($params);
        else
            $multiQuery = true;

        $bindParams = array();
        if ($types) {
            $bindParamsReferences = array();
            $bindParams = array_pad($bindParams,(count($params,1)-count($params))/count($params),"");
            foreach($bindParams as $key => $value)
                $bindParamsReferences[$key] = &$bindParams[$key];
            array_unshift($bindParamsReferences,$types);
            $bindParamsMethod = new ReflectionMethod('mysqli_stmt', 'bind_param');
            $bindParamsMethod->invokeArgs($stmt,$bindParamsReferences);
        }

        $result = array();
        foreach ($params as $queryKey => $query) {
            foreach ($bindParams as $paramKey => $value)
                $bindParams[$paramKey] = $query[$paramKey];
            $queryResult = array();
            if (mysqli_stmt_execute($stmt)) {
                $stmt0  = substr(strtolower($sql), 0, 6);
                if ($stmt0 == 'insert')
                    return mysqli_insert_id($link);
                elseif ($stmt0 != 'select')
                    return true;

                $resultMetaData = mysqli_stmt_result_metadata($stmt);
                $stmtRow = array();   
                $rowReferences = array();
                while ($field = mysqli_fetch_field($resultMetaData))
                    $rowReferences[] = &$stmtRow[$field->name];
                mysqli_free_result($resultMetaData);

                $bindResultMethod = new ReflectionMethod('mysqli_stmt', 'bind_result');
                $bindResultMethod->invokeArgs($stmt, $rowReferences);

                while (mysqli_stmt_fetch($stmt)) {
                    $row = array();
                    foreach ($stmtRow as $key => $value)
                        $row[$key] = $value;
                    $queryResult[] = $row;
                }
                mysqli_stmt_free_result($stmt);

            } else
                $queryResult[] = false;

            $result[$queryKey] = $queryResult;
        }
        mysqli_stmt_close($stmt);
    } else
        $result = false;

    if ($multiQuery)
        return $result;
    else
        return $result[0];
}

错误消息:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2128 bytes) in /mysite/scripts/db.php on line 200

抛出的错误行200是:

while (mysqli_stmt_fetch($stmt)) {
当结果大于2450个数据行时,会出现

错误。

0 个答案:

没有答案