php/sql..我应该如何使用50mb文件中的数据


php/sql ...how am i supposed to use data from a 50mb file?

到目前为止,我的代码如下。我走对了吗?我可以读取1mb的文件,但50mb会变得疯狂和缓慢。也许我不应该尝试输出全部数据?但我至少读对了吗?以下是适用于测试1mb文件但不适用于pubs.txt 50mb文件的指南和我的代码。

Write a PHP program that reads the posted pubs.txt file, parses it, and inserts the 
data into MySql tables. 
pubs.txt contains many publications. Each <pub> … </pub> pair specifies a 
publication, with ID, title, year, journal (<booktitle>), pages, and authors 
information. Some information may be missing. It is your own choice to use a 
default value or NULL for missing fields. Some information looks incorrect but 
you do not have to worry about it. The data in pubs.txt was automatically 
extracted from web resources by computer. 
You have the freedom to design the MySql database, as long as you can answer 
the queries correctly and hopefully efficiently. 
It is your own choice to execute this program from command line or web browser. 
3. Use PHP to write a web interface, which should provide intuitive forms allowing 
users to:
• Insert a publication into the database
• Query all publications by a particular author
• Query all publications in a particular year 

下面是我的php代码。感谢您的指导。

<?php

$mysqli = new mysqli('localhost', 'root', '', 'db1');
if (mysqli_connect_errno()){
printf("connect failed'n", mysqli_connect_error());
exit();    
}

error_reporting(E_ALL);
$header = '<?xml version="1.0" encoding="UTF-8"?>'."'n<datalist>";
$content = $header."'n".file_get_contents("pubs.txt")."'n</datalist>";
$ob = simplexml_load_string($content);
$json = json_encode($ob);    
$array = json_decode($json, true);
$alldata = $array["pub"];


foreach ($alldata as $key => $value) { //access all data in loop
$id = $value["ID"];
$title = $value["title"];
$year = $value["year"];
$booktitle = $value["booktitle"];
$pages = $value["pages"];
$authors = implode(",", $value["authors"]["author"]);

$stmt = $mysqli->prepare("INSERT INTO pubs VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param('ssssss',
               $value["ID"],
               $value["title"],
               $value["year"],
               $value["booktitle"],
               $value["pages"], implode(",", $value["authors"]["author"]));
$stmt->execute();
printf("%d row insrt'n", $stmt->affected_rows);
echo "<table>
<tr>
<th>ID</th>
<th>title</th>
<th>year</th>
<th>booktitle</th>
<th>pages</th>
<th>authors</th>
</tr>";
echo "<tr>";
echo "<td>" . $value['ID'] . "</td>";
echo "<td>" . $value['title'] .  "</td>";
echo "<td>" . $value['year'] .  "</td>";
echo "<td>" . $value['booktitle'] .  "</td>";
echo "<td>" . $value['pages'] . "</td>";
echo "<td>" . $value['authors'] . "</td>";
echo "</tr>";

}
echo "</table>";
?>

对于这么大的文档,您应该使用渐进式XML解析器,它不依赖于一次加载和解析所有文档。