国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > php数据库操作--数据预处理、更新、删除

php数据库操作--数据预处理、更新、删除

来源:程序员人生   发布时间:2017-03-07 09:01:19 阅读次数:3578次

终究又开始继续写博客了,离回家不远了 不知道大家的票有无买下,希望大家都可以回家过个团圆年。

本文简单讲述“如题”

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

创建:

//创建预处理
$createinto=$connent->prepare("insert into zh(name,age,email) values (?,?,?)");

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

//绑定
$createinto->bind_param("sis",$name,$age,$email);

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

$name="zhanghao1";
$age=1;
$email="1234123123@qq.com";
$createinto->execute();

$name="zhanghao2";
$age=2;
$email="1234123123@qq.com";
$createinto->execute();
履行语句;最后数据插入成功。(条件是连接到数据库并使用)



删除指定条目:

mysqli_query($connent,"delete from zh where name='zhanghao1'");
不加where条件删除全部表数据

更新指定条目:

mysqli_query($connent,"update zh set age=3 where name='zhanghao2'");
修改zhanghao2的年龄为3

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

----完全代码-------

<?php
/*
 * 数据预处理  删除 更新数据
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

 /**
  * 预处理 简单化  创建1种方法 多处使用
  */
//先连接数据库
$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是判断条件的根本,根据他我们可以条件查询,条件删除和条件修改。




生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生