You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MOODLE(PHP)测验模板仅输出部分记录的技术求助

Troubleshooting: Only a Few Records Output When Generating Moodle Quiz from MySQL Table

Hey there, let's walk through the possible issues that might be causing your script to only output 4 full records (and part of the 5th) when generating a Moodle quiz XML from your 9000-row testbank table. You mentioned adjusting echo spacing helps a bit and switching to mysqli_query is on your list—here are other key areas to check:

1. PHP Memory Limit Hit

Processing 9000 rows and building XML output can eat up more memory than PHP's default limit allows. Try bumping up the memory at the start of your script:

ini_set('memory_limit', '512M'); // Adjust based on your server's capacity

This gives the script enough space to hold the query results and construct the XML without getting killed prematurely.

2. Script Execution Timeout

If your script takes longer than PHP's max_execution_time to run, the web server will cut it off. Extend the timeout with:

set_time_limit(300); // 5 minutes—tweak as needed

Note: This doesn't apply if you're running the script via CLI, but it's critical for web-based execution.

3. Batch Processing Instead of Loading All Rows

The old mysql_* functions (which you're using) can struggle with huge result sets loaded all at once. Instead of fetching every row into memory, process them in batches:

$batchSize = 100; // Process 100 rows at a time
$offset = 0;

do {
    $result = mysql_query("SELECT line, process, question, reference, ans1, ans2, ans3, ans4, TRIM(TRAILING '\r' FROM correct) AS correct FROM testbank LIMIT $offset, $batchSize");
    $rowsReturned = mysql_num_rows($result);
    
    while ($row = mysql_fetch_assoc($result)) {
        // Your existing XML generation code goes here
    }
    
    $offset += $batchSize;
} while ($rowsReturned === $batchSize);

This reduces memory strain significantly and prevents the script from choking on a massive dataset.

4. Output Buffer Overflow

Web servers often limit the size of the output buffer. If your XML is too large, it gets truncated mid-output. Fix this by flushing the buffer after each question:

// After closing the </question> tag, add:
ob_flush();
flush();

This pushes the generated XML to the browser immediately instead of storing it all in memory until the end.

5. Malformed XML from Special Characters

If the 5th record has unescaped XML special characters (like <, >, &, or even ]]> inside CDATA), it can break the XML structure and cause the output to stop abruptly. Escape all dynamic content before outputting:

// Escape text for XML elements
echo ' <text>' . htmlspecialchars($row['line'] . ' ' . $row['process'], ENT_XML1) . '</text>' . "\r\n";

// For CDATA sections, handle any ]]> sequences to avoid breaking the structure
$questionContent = str_replace(']]>', ']]]]><![CDATA[>', $row['question']);
$referenceContent = str_replace(']]>', ']]]]><![CDATA[>', $row['reference']);
echo " <![CDATA[(".htmlspecialchars($row['line']." ".$row['process'], ENT_XML1)." ") ".$questionContent." <!-- ".$referenceContent." -->]]>" . "\r\n";

This ensures your XML stays valid and doesn't get cut off due to syntax errors.

6. Hidden Errors Stopping the Script

Your current script doesn't have error handling, so a small issue (like a missing field or invalid data) could crash the script silently. Add error logging and display to catch these:

// At the top of your script (for development only!)
ini_set('display_errors', 1);
error_reporting(E_ALL);

// Inside your row loop
while ($row = mysql_fetch_assoc($result)) {
    try {
        // Your XML generation code here
    } catch (Exception $e) {
        error_log("Failed processing record line {$row['line']}: " . $e->getMessage());
        continue; // Skip the bad row and keep going
    }
}

This will help you pinpoint exactly which record or line of code is causing the script to stop.


内容的提问来源于stack exchange,提问作者Mr_Thomas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:54:28