用php从文件中填充mysql数据库

gxwragnw  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(192)

这个问题在这里已经有答案了

php解析/语法错误;以及如何解决这些问题(18个答案)
两年前关门了。
我正在尝试使用文件中包含的数据填充数据库表。该文件包含csv格式的数据。我想我真的很接近,但我不太明白。我也试着研究explode()函数,但没能成功。
谁有更多的经验一起使用php和sql可以提供一些建议?提前谢谢。
代码的第41-60行与填充表直接相关。代码顶部提到的db\u connect.php文件可以工作。其余的代码也可以工作。在尝试实现这个从文件读取函数之前,我通过硬编码数据填充表来测试程序。
现在,我得到一个错误,上面写着“parse error:syntax error,unexpected end of file at line 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>

这是文件中包含的数据片段。该文件包含20000行数据。

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

截图1
截图2-只是一个looooong列表的开始哈哈
截图3

2nbm6dog

2nbm6dog1#

好吧,我能解决我的问题。我的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>

我是相当新的堆栈溢出,所以我不知道如何回答你自己的问题是看。我可以想象这是好的,因为它让人们知道问题已经解决,因此他们不必浪费时间在你的问题上。

相关问题