插入多条数据

发布时间:2019-10-06  栏目:编程  评论:0 Comments

1.环境部署

PHP MySQL 插入多条数据


PHP MySQL 插入多条数据


XXamp是真的强,简化了mysql后,继承到这里,虽然,往后发展局限性很强不过这完全无所谓,

使用 MySQLi 和 PDO 向 MySQL 插入多条数据

mysqli_multi_query() 函数可用来执行多条SQL语句。

以下实例向 “MyGuests” 表添加了三条新的记录:

使用 MySQLi 和 PDO 向 MySQL 插入多条数据

mysqli_multi_query() 函数可用来执行多条SQL语句。

以下实例向 “MyGuests” 表添加了三条新的记录:

本来就是主打的小中型项目。

实例 (MySQLi – 面向对象)

<?php
$servername = “localhost”;
$username = “username”;
$password =
“password”;
$dbname =
“myDB”;

// 创建链接
$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 – 面向对象)

<?php
$servername = “localhost”;
$username = “username”;
$password =
“password”;
$dbname =
“myDB”;

// 创建链接
$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语句必须用分号隔开。

 

所以如果说是环境部署,如果配置了PHP的基本上都已经搭建好了!

实例 (MySQLi – 面向过程)

<?php
$servername = “localhost”;
$username = “username”;
$password =
“password”;
$dbname =
“myDB”;

// 创建链接
$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);
?>

 

实例 (MySQLi – 面向过程)

<?php
$servername = “localhost”;
$username = “username”;
$password =
“password”;
$dbname =
“myDB”;

// 创建链接
$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);
?>

 

2.数据库起手

实例 (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’)”);

// 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;
?>

 


实例 (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’)”);

// 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;
?>

 


1.链接数据库的语句

使用预处理语句

mysqli 扩展提供了第二种方式用于插入语句。

我们可以预处理语句及绑定参数。

mysql 扩展可以不带数据发送语句或查询到mysql数据库。 你可以向列关联或
“绑定” 变量。

使用预处理语句

mysqli 扩展提供了第二种方式用于插入语句。

我们可以预处理语句及绑定参数。

mysql 扩展可以不带数据发送语句或查询到mysql数据库。 你可以向列关联或
“绑定” 变量。

2.需要执行的语句

Example (MySQLi 使用预处理语句)

<?php
$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);

//预处理语句

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 参数是字符串。

This argument may be one of four types:

  • i – integer

  • d – double

  • s – string

  • b – BLOB

每个参数必须指定类型,来保证数据的安全性。通过类型的判断可以减少SQL注入漏洞带来的风险。

Example (MySQLi 使用预处理语句)

<?php
$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);

//预处理语句

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 参数是字符串。

This argument may be one of four types:

  • i – integer

  • d – double

  • s – string

  • b – BLOB

每个参数必须指定类型,来保证数据的安全性。通过类型的判断可以减少SQL注入漏洞带来的风险。

3.两者之间的联系

4.关闭数据库

/**

* Created by PhpStorm.

* User: Administrator

* Date: 2018/9/20 0020

* Time: 18:15

*/

$servername = “localhost”;

$username = “root”;

$password = “”;

/*—————–mysqli创建数据库———————*/

//////创建链接

//$conn=new mysqli($servername,$username,$password);

//

/////测试链接

//

//if ($conn->connect_error)

//{

// die(“链接失败:”.$conn->connect_error);

//}

//echo “链接成功”.PHP_EOL;

//

/////创建数据库

//

//$sql=”CREATE DATABASE myDB”;

//

//if (mysqli_query($conn,$sql))

//{

// echo “创建成功”;

//}

//else {

//

// echo “请重试”.mysqli_connect_error();

// }

//mysqli_close;

/*———————-pdo来创建数据库————————*/

try {

///创建链接

$conn = new PDO(“mysql:host=$servername”, $username, $password);

///数据库语句

$sql = “CREATE DATABASE thebase”;

///执行数据库语句

$conn->exec;

echo “再次创建成功”;

}

catch (PDOException $e)

{

echo “创建失败”.$e->getMessage();

}

?>

以上便是,从链接数据库到创建数据库的过程,两个办法,或一个办法的,如果程序出错了的话

可以比较清楚的知道出错部分在哪里!

2.数据库的增删改查

/**

* Created by PhpStorm.

* User: lck

* Date: 2018/9/22 0022

* Time: 11:28

留下评论

网站地图xml地图