【问题标题】:Populating MySQL Database from file using PHP [duplicate]使用 PHP 从文件中填充 MySQL 数据库 [重复]
【发布时间】:2018-04-30 00:41:34
【问题描述】:

我正在尝试使用文件中包含的数据填充数据库表。该文件包含 CSV 格式的数据。我想我真的很接近,但我无法弄清楚。我也尝试研究了 explode() 函数,但我无法让它工作。

有更多经验的人可以同时使用 PHP 和 SQL 提供一些建议吗?提前谢谢。

第 41-60 行代码与填充表格直接相关。代码顶部提到的 db_connect.php 文件有效。其余的代码也可以工作。在尝试实现从文件读取功能之前,我通过硬编码数据来填充表格来测试程序。

目前,我收到一条错误消息,上面写着“解析错误:语法错误,第 103 行的文件意外结束”,通过谷歌搜索意味着我的 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>

这是文件中包含的数据的 sn-p。该文件包含 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

Screenshot 1

Screenshot 2 - Just the beginning of a loooooooong list lol

Screenshot 3

【问题讨论】:

  • 我没有看到表格。
  • 我需要一个表格来存储文件中的数据吗?
  • 是的,对于 $_POST 和 $_FILES 超全局数组。错误报告应该向您抛出未定义的索引通知,但您没有设置系统来捕获它们。
  • 另外,文件需要 POST 方法以及正确的 enctype。
  • 感谢您提供的所有信息。我在做笔记哈哈

标签: php mysql sql file mysqli


【解决方案1】:

好的,我能够解决我的问题。我的 PHP 代码现在可以正确读取 CSV 文件并填充 MySQL 数据库表。这是上面与问题相关的代码的sn-p。

<?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 还很陌生,所以我不知道如何回答您自己的问题。我想这没关系,因为它可以让人们知道问题已经解决,因此他们不必在您的问题上浪费时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    • 2012-05-28
    • 2017-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多