我是靠谱客的博主 合适小懒猪,这篇文章主要介绍php数据库操作--数据预处理、更新、删除,现在分享给大家,希望可以做个参考。

语句预处理:通俗的就是一次查询,多次执行,在我们后期的项目中会经常用到

创建:

//创建预处理

复制代码
1
$createinto=$connent->prepare("insert into zh(name,age,email) values (?,?,?)");
登录后复制

sql语句,参数使用?代替为预留

复制代码
1
2
//绑定 $createinto->bind_param("sis",$name,$age,$email);
登录后复制

绑定参数 s为String类型 i为int类型

复制代码
1
2
3
4
5
6
7
8
9
$name="zhanghao1"; $age=1; $email="1234123123@qq.com"; $createinto->execute(); $name="zhanghao2"; $age=2; $email="1234123123@qq.com"; $createinto->execute();
登录后复制

执行语句;最后数据插入成功。(前提是连接到数据库并使用)

删除指定条目:

复制代码
1
mysqli_query($connent,"delete from zh where name='zhanghao1'");
登录后复制

不加where条件删除整个表数据

更新指定条目:

复制代码
1
mysqli_query($connent,"update zh set age=3 where name='zhanghao2'");
登录后复制

修改zhanghao2的年龄为3

全部数据库操作完之后要关闭数据库。

----完整代码-------

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php /* * 数据预处理 删除 更新数据 * * To change the template for this generated file go to * Window - Preferences - PHPeclipse - PHP - Code Templates */ /** * 预处理 简单化 创建一种方法 多处使用 */ //先连接数据库 $servername="localhost"; $username="root"; $userpassword="hao542161"; $dbname = "testdb"; $connent=new mysqli($servername,$username,$userpassword,$dbname); if($connent->connect_error){ die("连接失败: " . $connent->connect_error); }else{ echo "成功"; } //创建预处理 $createinto=$connent->prepare("insert into zh(name,age,email) values (?,?,?)"); //绑定 $createinto->bind_param("sis",$name,$age,$email); //多次执行 $name="zhanghao1"; $age=1; $email="1234123123@qq.com"; $createinto->execute(); $name="zhanghao2"; $age=2; $email="1234123123@qq.com"; $createinto->execute(); echo "插入成功"; //删除数据 删除表中 name为zhanghao1的数据 mysqli_query($connent,"delete from zh where name='zhanghao1'"); mysqli_query($connent,"update zh set age=3 where name='zhanghao2'"); $connent->close(); ?>
登录后复制

我们可以发现其中where是判断条件的根本,根据他我们可以条件查询,条件删除和条件修改。

最后

以上就是合适小懒猪最近收集整理的关于php数据库操作--数据预处理、更新、删除的全部内容,更多相关php数据库操作--数据预处理、更新、删除内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(109)

评论列表共有 0 条评论

立即
投稿
返回
顶部