一般情況下,INSERT 語句只能向 MySQL 數(shù)據(jù)庫添加一條語句,而本文將介紹如何使用函數(shù)批量的向數(shù)據(jù)表插入多條數(shù)據(jù)。
使用 MySQLi 和 PDO 向 MySQL 插入多條數(shù)據(jù)
mysqli_multi_query() 函數(shù)可用來執(zhí)行多條SQL語句。
以下實例向 "MyGuests" 表添加了三條新的記錄:
實例 (MySQLi - 面向?qū)ο?
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// 創(chuàng)建鏈接
$conn =
new mysqli($servername, $username, $password, $dbname);
// 檢查鏈接
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if ($conn->multi_query($sql) === TRUE) {
echo "New
records created successfully";
} else {
echo
"Error: " . $sql . "
" . $conn->error;
}
$conn->close();
?>
請注意,每個SQL語句必須用分號隔開。 |
實例 (MySQLi - 面向過程)
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// 創(chuàng)建鏈接
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// 檢查鏈接
if (!$conn) {
die("Connection
failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO
MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if (mysqli_multi_query($conn, $sql)) {
echo "New
records
created successfully";
} else {
echo "Error: "
. $sql . "
" . mysqli_error($conn);
}
mysqli_close($conn);
?>
實例 (PDO)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname =
"myDBPDO";
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);
// 開始事務(wù)
$conn->beginTransaction();
// SQL 語句
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')");
// commit the transaction
$conn->commit();
echo "New records created successfully";
}
catch(PDOException $e)
{
// roll back the transaction if something failed
$conn->rollback();
echo $sql . "
" . $e->getMessage();
}
$conn = null;
?>
使用預(yù)處理語句
mysqli 擴展提供了第二種方式用于插入語句。
我們可以預(yù)處理語句及綁定參數(shù)。
mysql 擴展可以不帶數(shù)據(jù)發(fā)送語句或查詢到mysql數(shù)據(jù)庫。 你可以向列關(guān)聯(lián)或 "綁定" 變量。
Example (MySQLi 使用預(yù)處理語句)
$servername = "localhost";
$username = "username";
$password =
"password";
$dbname =
"myDB";
// Create connection
$conn =
new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
$sql = "INSERT INTO
MyGuests (firstname, lastname, email) VALUES(?, ?, ?)";
// 為 mysqli_stmt_prepare() 初始化
statement 對象
$stmt =
mysqli_stmt_init($conn);
//預(yù)處理語句
if (mysqli_stmt_prepare($stmt, $sql)) {
// 綁定參數(shù)
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
// 設(shè)置參數(shù)并執(zhí)行
$firstname = 'John';
$lastname
= 'Doe';
$email =
'john@example.com';
mysqli_stmt_execute($stmt);
$firstname = 'Mary';
$lastname
= 'Moe';
$email =
'mary@example.com';
mysqli_stmt_execute($stmt);
$firstname = 'Julie';
$lastname = 'Dooley';
$email =
'julie@example.com';
mysqli_stmt_execute($stmt);
}
}
?>
我們可以看到以上實例中使用模塊化來處理問題。我們可以通過創(chuàng)建代碼塊實現(xiàn)更簡單的讀取和管理。
注意參數(shù)的綁定。讓我們看下 mysqli_stmt_bind_param() 中的代碼:
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
該函數(shù)綁定參數(shù)查詢并將參數(shù)傳遞給數(shù)據(jù)庫。第二個參數(shù)是 "sss" 。以下列表展示了參數(shù)的類型。 s 字符告訴 mysql 參數(shù)是字符串。
可以是以下四種參數(shù):
i - integer
d - double
s - string
b - BLOB
每個參數(shù)必須指定類型,來保證數(shù)據(jù)的安全性。通過類型的判斷可以減少SQL注入漏洞帶來的風(fēng)險。
更多建議: