获取json_encoded数组耗尽内存

时间:2017-08-20 03:35:03

标签: php mysql arrays json

我有两个来自表单输入的数组,我试图将它们组合并存储在一个数据库单元中。我能够做到这一点,但是当试图检索它时,它会给内存限制带来致命的错误。

我正在插入主滑块,其中包含其他幻灯片。的种类。我不想创建单独的sql表,但也许我需要。

我的记忆力是256mb,我知道我可以增加它,但我想知道它为什么这么大。

所以我的输入来自:

<fieldset class="form-group">
  <label class="label-top" for="scroll-content[]">Scroll Text #1</label>
  <textarea rows="5" cols="50" name="scroll-content[]" id="scroll-content-"></textarea>
  <input type="hidden" name="slide-number[]" value="1">
</fieldset>

<fieldset class="form-group">
  <label class="label-top" for="scroll-content[]">Scroll Text #2:</label>
  <textarea rows="5" cols="50" name="scroll-content[]" id="scroll-content-"></textarea>
  <input type="hidden" name="slide-number[]" value="2">
</fieldset>

插入:

function add_new_scroll($dir, $plugin_id) {

    // configure content array
    $num = $_POST['slide-number'];
    $array = $_POST['scroll-content'];
    $combined = array_combine($num, $array);

    $content = json_encode($combined);

    // global connection
    global $conn;
    wj_connect();

    // sql
    $sql = "INSERT INTO `scroll_text` (`scroll_order`, `num_slides`, `scroll_title`, `scroll_slug`, `scroll_content`)
                VALUES(?,?,?,?,?)
                ON DUPLICATE KEY UPDATE
                    `scroll_order` = VALUES(`scroll_order`),
                    `num_slides` = VALUES(`num_slides`),
                    `scroll_title` = VALUES(`scroll_title`),
                    `scroll_slug` = VALUES(`scroll_slug`),
                    `scroll_content` = VALUES(`scroll_content`)";

    if ($stmt = $conn->prepare($sql)) {

        $stmt->bind_param("iisss", $ansp_order, $ansp_num_slides, $ansp_title, $ansp_slug, $ansp_content);

        // set params
        $ansp_order = 0;
        $ansp_num_slides = 1;
        $ansp_title = $_POST['scroll-title'];
        $ansp_slug = $_POST['scroll-slug'];
        $ansp_content = $content;

        $stmt->execute();
        $stmt->close();

    } else {
        echo 'Scroll Text not added.';
    }

    $conn->close();
    header("Location: ". $dir . "/scroll-text-admin.php?plug_id=" . $plugin_id . "&type=edit&slug=" . $ansp_slug);
}

商店如:

{"1":"<p>1<\/p>","2":"<p>2<\/p>"}

检索:

function return_scroll() {

    // global connection
    global $conn;
    wj_connect();

    // sql
    $sql = "SELECT `id`, `scroll_order`, `num_slides`, `scroll_title`, `scroll_slug`, `scroll_content`
                FROM `scroll_text` WHERE `scroll_slug` = ? LIMIT 1";

    if ($stmt = $conn->prepare($sql)) {

        $stmt->bind_param("s", $rsp_slug);
        $rsp_slug = $_GET['slug'];

        $stmt->execute();

        // bind results
        $stmt->bind_result($rsr_id, $rsr_order, $rsr_num_slides, $rsr_title, $rsr_slug, $rsr_content);

        $scroll = array(
            'id' => $rsr_id,
            'order' => $rsr_order,
            'num_slides' => $rsr_num_slides,
            'title' => $rsr_title,
            'slug' => $rsr_slug,
            'content' => $rsr_content
            );

        $stmt->fetch();
        $stmt->close();
    }

    $conn->close();

    return $scroll;
}

错误:

Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4294967296 bytes) in /home/wonderadmin/public_html/wj-admin/plugins/scroll-text/scroll-text.php on line 199

1 个答案:

答案 0 :(得分:0)

我的MySQL数据库行类型是longtext。将其更改为中等文本,我很好。尴尬。