php怎么增加多行数据
时间:2022-02-11 13:57
php增加多行数据的方法:1、创建一个PHP示例文件;2、连接数据库;3、通过mysqli_multi_query()函数执行多条SQL语句并使用INSERT语句向数据表中增加多行数据即可。 本文操作环境:windows7系统、PHP7.1版、DELL G3电脑 php怎么增加多行数据? mysqli_multi_query() 函数可用来执行多条SQL语句。 以下实例向 "MyGuests" 表添加了三条新的记录: 实例 (MySQLi - 面向对象) Note 请注意,每个SQL语句必须用分号隔开。 实例 (MySQLi - 面向过程) 推荐学习:《PHP视频教程》 以上就是php怎么增加多行数据的详细内容,更多请关注gxlsystem其它相关文章!<?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();
?>
<?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);
?>