简体   繁体   English

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

[英]php/sql …how am i supposed to use data from a 50mb file?

my code so far is below. 到目前为止,我的代码如下。 am i on the right track? 我在正确的轨道上吗? i was able to read in a 1mb file but 50mb gets crazy and slow. 我能够读取1mb的文件,但是50mb变得疯狂而缓慢。 perhaps i shouldnt be trying to output the entire data? 也许我不应该尝试输出整个数据? but am i atleast reading it in correctly? 但是我至少能正确阅读吗? below are guidelines and my code that works on a test 1mb file but not the pubs.txt 50mb file. 以下是适用于测试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 

below is my php code . 下面是我的PHP代码。 thanks for any guidance. 感谢您的指导。

<?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解析器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM