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. 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.
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 . 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>";
?>