PHP MySQL? ?? ??? ??? ?????.
?? ???? ??? ???? ???? ??? ???? ??? ?????. ? ?? ?? ?? ???? ??? ? ???? ??? ?? ????? ?? ?? ???? ???? ??? ?????. ???? ? ??.
mysqli_multi_query() ??? ???? ?? SQL ?? ??? ? ????.
??? ?? ?? ????
????
?? "MyGuests" ???? 3?? ? ???? ??????:
<?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; // 創(chuàng)建鏈接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 檢查鏈接 if (!$conn) { die("連接失敗: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('tom', 'Doe', '12032047@asd.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); ?>
???? ?? ??:
? ???? ????? ???????.
??? ???? ?? ?????.
? ?? ??? ???? ?????
??? ? ??
mysqli ??? ?? ???? ? ?? ??? ?????.
?? ???? ????? ???? ? ????.
mysql ??? ??? ?? mysql ??????? ????? ??? ?? ? ????. ??? ????? ?????? "???"? ? ????.
????
??? ? ??
<?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; // 創(chuàng)建連接 $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); //預(yù)處理語句 if (mysqli_stmt_prepare($stmt, $sql)) { // 綁定參數(shù) mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email); // 設(shè)置參數(shù)并執(zhí)行 $firstname = 'liu'; $lastname = 'Doe'; $email = 'john@example.com'; mysqli_stmt_execute($stmt); $firstname = 'zhang'; $lastname = 'Moe'; $email = 'mary@example.com'; mysqli_stmt_execute($stmt); $firstname = 'li'; $lastname = 'Dooley'; $email = 'julie@example.com'; mysqli_stmt_execute($stmt); } } ?>
??? ??VALUES ??? ?? ???? ??? with? ??, ??? ? ?? ????? , ??? ? mysqli_stmt_bind_param($stmt, 'sss', $firstname, $lastname, $email), ? ?? ?????? ?? ?? "s"? ??? ?? ? ?? ????? ??? ??? ?????. string?? ??? ?? ??? ????.
· i - ??
· d - ???? ?? ??? ?
· s - ???
· b - ?? ?
?? ??? ?????? ?? ?? ???? ??? ? ?? ??? VARCHAR ??? ????? ???? ??? ? "S"?? ???. ?? ??? int ???? "i"?? ???.
? ????? ??? ??? ???? ?? ??? ???? ???. ?? ??? ?? SQL ?? ???? ??? ?? ? ????.
?? ????? PHP? ??? ??? ???????.