我正在尝试使用PHP脚本从文本文件向数据库表中添加条目。文本文件包含2000行,我希望每行都是数据库表中的一个条目。这是我的代码。它运行正常但没有添加到表中。
<?php
$link = new mysqli('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx');
if ($link->connect_errno) {
die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
$filename = 'serials.txt';
$file = file($filename);
foreach($file as $line) {
$result = $link->query("INSERT INTO mytable SET serial=$line");
}
?>
答案 0 :(得分:2)
<?php
$link = new mysqli('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx');
if ($link->connect_errno) {
die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
$filename = 'serials.txt';
$file = file($filename);
foreach($file as $line) {
$result = $link->query("INSERT INTO mytable SET serial='"
.$link->real_escape_string($line)."'");
if($link->errno) echo($link->errno.": ".$link->error);
}
?>