PHP MySQL 插入多條數據
使用 MySQLi 和 PDO 向 MySQL 插入多條資料
mysqli_multi_query() 函數可用於執行多條SQL語句。
相關影片教學推薦:《mysql教學》http://www.php.cn/course/list/51.html
以下實例為"MyGuests" 資料表新增了三個新的記錄:
#實例(MySQLi - 物件導向)
<?php $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', '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 "新记录插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
##請注意,每個SQL語句必須用分號隔開。
實例(MySQLi - 面向程序)
<?php
$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', '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 "新记录插入成功";
} else {
echo "Error: "
. $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
實例(PDO)
<?php $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', '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')"); // 提交事务 $conn->commit(); echo "新记录插入成功"; } catch(PDOException $e) { // 如果执行失败回滚 $conn->rollback(); echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
使用預處理語句mysqli 擴充功能提供了第二種方式用於插入語句。
我們可以預處理語句及綁定參數。
mysql 擴充功能可以不帶資料傳送語句或查詢到mysql資料庫。 你可以向列關聯或 "綁定" 變數。
實例 (MySQLi 使用預處理語句)<?php $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 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 = '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); } } ?>
我們可以看到上述實例中使用模組化來處理問題。我們可以透過建立程式碼區塊來實現更簡單的讀取和管理。 注意參數的綁定。讓我們看下 mysqli_stmt_bind_param() 中的程式碼:mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email);
該函數綁定參數查詢並將參數傳遞給資料庫。第二個參數是 "sss" 。以下列表展示了參數的類型。 s 字元告訴 mysql 參數是字串。
可以是以下四個參數:- i - 整數
- d - 雙重精確度浮點數