我正在尝试使用文件中包含的数据填充数据库表。该文件包含CSV格式的数据。我觉得我很亲密,但我无法弄明白。我试着查看explode()函数,但是我无法使它工作。
拥有使用PHP和SQL的经验的人是否能够提供一些建议?提前谢谢。
第41-60行代码与填充表格直接相关。代码顶部引用的db_connect.php文件有效。其余的代码也可以。在尝试从文件函数实现此读取之前,我通过硬编码数据来测试程序以填充表。
目前,我收到一条错误,上面写着"解析错误:语法错误,第103行和第34行的文件意外结束;通过做一些谷歌搜索意味着我的PHP中有错误。
更新:我能够解决它。我已经更新了下面的代码并包含了一些截图。感谢那些插话并提出一些建议的人。
<?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>
这是文件中包含的数据片段。该文件包含20,000行数据。
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
答案 0 :(得分:0)
好吧,我能够解决我的问题。我的PHP代码现在正在读取CSV文件并填充MySQL数据库表。以下是与上述问题相关的代码片段。
<?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>
我对Stack Overflow很新,所以我不知道如何回答你自己的问题。我认为它没关系,因为它让人们知道问题已经解决,因此他们不必浪费时间在你的问题上。