嘿伙计们,所以这是我的桌子:
CREATE TABLE mytable (
ID int AUTO_INCREMENT,
Name varchar(255) NOT NULL,
PRIMARY KEY (ID));
有我的代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// begin the transaction
$conn->beginTransaction();
// our SQL statements
$size = "";
$dir = "test";
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if($file == '.' || $file == '..') continue;
$myFile = "$dir/$file";
$fh = fopen($myFile, 'r');
$size = filesize($myFile);
if ($size > 0) {
$content = fread($fh, $size);
$title = $file;
echo $title."</br>";
}
$models = explode("\r",$content);
$conn->exec("INSERT INTO `mytable` (`Name`)
VALUES".$models);
fclose($fh);
}
closedir($handle);
}
// commit the transaction
$conn->commit();
echo "Succsess";
}
catch(PDOException $e)
{
// roll back the transaction if something failed
$conn->rollback();
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
以下是我的文本文件的外观:
4745G
VN7-572
E5-511G
VN7-571....
我一直收到此错误:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法出错;
任何人都知道我做错了什么以及如何解决它?