我有一个包含数千个元素的数组。这是将它放入数据库的正确方法:
$connection = mysqli_connect($url, $usr, $pwd, $db) or die('<br><br>Error: ' . mysqli_error() . "<br><br>");
$stmt=mysqli_stmt_init($connection);
mysqli_stmt_prepare($stmt,"INSERT INTO Bible_KJV (verse) VALUES(?)");
$out .= "<br><hr><br>";
//Inserting the book in the database
for($i = 0;$i < count($lines);$i++) {
mysqli_stmt_bind_param($stmt,"s",$lines[$i]);
mysqli_stmt_execute($stmt);
$versenr = $i+1;
$out .= "Verse nr.: $versenr was inserted <br>";
}
答案 0 :(得分:1)
还有批量插入解决方案。如果php和MySQL共享相同的文件系统,您可以执行以下操作:
$filename=.... /* insert a temp filename here */
$connection = mysqli_connect($url, $usr, $pwd, $db) or die('<br><br>Error: ' . mysqli_error() . "<br><br>");
file_put_contents($filename,implode('\n',$lines));
mysqli_query($connection,"LOAD DATA INFILE '$filename' INTO TABLE `Bible_KJV`");
unlink($filename); // cleanup