PHP MySQL 插入多條資料
使用 MySQLi 和 PDO 向 MySQL 插入多條資料
mysqli_multi_query() 函式可用來執行多條SQL語句。
以下例項向 "MyGuests" 表添加了三條新的記錄:
例項 (MySQLi - 面向物件)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 建立連結
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢查連結
if ($conn->connect_error) {
die("連線失敗: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";
if ($conn->multi_query($sql) === TRUE) {
echo "新記錄插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
請注意,每個SQL語句必須用分號隔開。 |
例項 (MySQLi - 面向過程)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 建立連結
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 檢查連結
if (!$conn) {
die("連線失敗: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";
if (mysqli_multi_query($conn, $sql)) {
echo "新記錄插入成功";
} else {
echo "Error: " . $sql . "<br>" . 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);
// 開始事務
$conn->beginTransaction();
// SQL 語句
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]')");
$conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')");
// 提交事務
$conn->commit();
echo "新記錄插入成功";
}
catch(PDOException $e)
{
// 如果執行失敗回滾
$conn->rollback();
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
使用預處理語句
mysqli 擴充套件提供了第二種方式用於插入語句。
我們可以預處理語句及繫結引數。
mysql 擴充套件可以不帶資料傳送語句或查詢到mysql資料庫。 你可以向列關聯或 "繫結" 變數。
例項 (MySQLi 使用預處理語句)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 建立連線
$conn = new mysqli($servername, $username, $password, $dbname);
// 檢測連線
if ($conn->connect_error) {
die("連線失敗: " . $conn->connect_error);
} else {
$sql = "INSERT INTO MyGuests(firstname, lastname, email) VALUES(?, ?, ?)";
// 為 mysqli_stmt_prepare() 初始化 statement 物件
$stmt = mysqli_stmt_init($conn);
//預處理語句
if (mysqli_stmt_prepare($stmt, $sql)) {
// 繫結引數
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
// 設定引數並執行
$firstname = 'John';
$lastname = 'Doe';
$email = '[email protected]';
mysqli_stmt_execute($stmt);
$firstname = 'Mary';
$lastname = 'Moe';
$email = '[email protected]';
mysqli_stmt_execute($stmt);
$firstname = 'Julie';
$lastname = 'Dooley';
$email = '[email protected]';
mysqli_stmt_execute($stmt);
}
}
我們可以看到以上例項中使用模組化來處理問題。我們可以通過建立程式碼塊實現更簡單的讀取和管理。
注意引數的繫結。讓我們看下 mysqli_stmt_bind_param() 中的程式碼:
mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
該函式繫結引數查詢並將引數傳遞給資料庫。第二個引數是 "sss" 。以下列表展示了引數的型別。 s 字元告訴 mysql 引數是字串。
可以是以下四種引數:
- i - 整數
- d - 雙精度浮點數
- s - 字串
- b - 布林值
每個引數必須指定型別,來保證資料的安全性。通過型別的判斷可以減少SQL注入漏洞帶來的風險。