I am attempting to populate a database table using the data contained within a file. The file contains data in CSV format. I think I am really close but I cannot quite figure it out. I tried looking into the explode() functions as well but I could not make it work.
Would anybody with more experience using PHP and SQL together be able to provide some suggestions? Thanks ahead of time.
Lines 41-60 of code are the ones directly related to populating the table. The db_connect.php file referred to at the top of the code works. The rest of the code works as well. I tested the program by hardcoding data to populate the table before trying to implement this reading from file function.
At the moment, I am getting a error that says "Parse error: syntax error, unexpected end of file at line 103" which from doing some googling means I have an error in my PHP.
UPDATE: I was able to fix it. I have updated the code below and included some screenshots. Thank you to those of you that chimed in and provided some suggestions.
<?php
require_once './php/db_connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BABYNAMES Table Test</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>BABYNAMES Table Test</h1>
</div>
<?php
// Create table with three columns: Name, Gender, and Count
$createStmt = 'CREATE TABLE `BABYNAMES` (' . PHP_EOL
. ' `Name` varchar(255),' . PHP_EOL
. ' `Gender` char(1),' . PHP_EOL
. ' `Count` int NOT NULL,' . PHP_EOL
. ' PRIMARY KEY (`Name`, `Gender`)' . PHP_EOL
. ') ENGINE=MyISAM DEFAULT CHARSET=latin1;';
?>
<div id="step-one" class="well">
<h3>Step One <small>Creating the table</small></h3>
<pre><?php echo $createStmt; ?></pre>
<?php
if($db->query($createStmt)) {
echo ' <div class="alert alert-success">Table creation successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table creation failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit(); // Prevents the rest of the file from running
}
?>
</div>
<?php
// Read data from yob2016.txt into BABYNAMES table
$query = "LOAD DATA LOCAL INFILE 'yob2016.csv'
INTO TABLE BABYNAMES
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(Name,Gender,Count)";
?>
<div id="step-two" class="well">
<h3>Step Two <small>Inserting into the table</small></h3>
<pre><?php echo $query; ?></pre>
<?php
if($db->query($query)) {
echo ' <div class="alert alert-success">Values inserted successfully.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Value insertion failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit();
}
?>
</div>
<?php
// Get the rows from the table
$selectStmt = 'SELECT * FROM `BABYNAMES`;';
?>
<div id="step-three" class="well">
<h3>Step Three <small>Retrieving the rows</small></h3>
<pre><?php echo $selectStmt; ?></pre>
<?php
$result = $db->query($selectStmt);
if($result->num_rows > 0) {
echo ' <div class="alert alert-success">' . PHP_EOL;
while($row = $result->fetch_assoc()) {
echo ' <p>Name: ' . $row["Name"] . ' - Gender: ' . $row["Gender"] . ' - Count: ' . $row["Count"] . '</p>' . PHP_EOL;
}
echo ' </div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
</div>
<?php
// Drop the TEST table now that we're done with it
$dropStmt = 'DROP TABLE `BABYNAMES`;';
?>
<div id="step-four" class="well">
<h3>Step Four <small>Dropping the table</small></h3>
<pre><?php echo $dropStmt; ?></pre>
<?php
if($db->query($dropStmt)) {
echo ' <div class="alert alert-success">Table drop successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table drop failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit();
}
?>
</div>
</div>
</body>
</html>
This is a snippet of the data contained within the file. The file contains 20,000 lines of data.
Emma,F,19414
Olivia,F,19246
Ava,F,16237
Sophia,F,16070
Isabella,F,14722
Mia,F,14366
Charlotte,F,13030
Abigail,F,11699
Emily,F,10926
Harper,F,10733
Amelia,F,10702
Evelyn,F,10060
Elizabeth,F,9493
Sofia,F,9134
Madison,F,8982
Avery,F,8733
Ella,F,7866
Scarlett,F,7680