1

我有一个包含数千个元素的数组。这是将其放入数据库的正确方法:

$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>";
}
4

1 回答 1

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
于 2013-10-22T18:58:47.003 回答