要導(dǎo)入大量數(shù)據(jù)到數(shù)據(jù)庫,可以使用PHP中的數(shù)據(jù)庫擴(kuò)展(如MySQLi或PDO)來執(zhí)行批量插入操作。以下是一個(gè)示例代碼來導(dǎo)入大量數(shù)據(jù)到數(shù)據(jù)庫:
// 連接數(shù)據(jù)庫
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// 準(zhǔn)備要插入的數(shù)據(jù)
$data = [
['John', 'Doe', 'john.doe@example.com'],
['Jane', 'Smith', 'jane.smith@example.com'],
// 更多數(shù)據(jù)...
];
// 構(gòu)建SQL語句
$sql = "INSERT INTO users (first_name, last_name, email) VALUES ";
$values = array_map(function($row) {
return "('" . implode("','", $row) . "')";
}, $data);
$sql .= implode(", ", $values);
// 執(zhí)行插入操作
if ($conn->query($sql) === TRUE) {
echo "Records inserted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// 關(guān)閉連接
$conn->close();
請注意,這只是一個(gè)示例代碼,實(shí)際使用時(shí)需要根據(jù)實(shí)際情況修改代碼。確保輸入的數(shù)據(jù)已經(jīng)經(jīng)過適當(dāng)?shù)尿?yàn)證和清理,以避免SQL注入攻擊。