php中的增删查改
php操作SQL
1、 数据库中的增删查改
1.1、查询
语句:select
什么内容from
表名 where
判断条件
查询可以返回一个结果集,是一个列表
查询在这张表里面的所有id列和name列 select id,name from mytable 带条件的查询 select * from mytable where id = 3 查询年龄小于20岁的 select * from mytable where age < 20 查询年龄小于20岁,性别为女生 and or not select * from mytable where age < 20 and gender = 0
1.2、增加
语句:insert [into]
表名[(字段1, 字段2,…)] values
(值1,值2)
-- 表名后没有指定字段,那么必须设置对应数量的值,并且主键不能重复 insert into mytable values('lili', 30, 0) -- 没有id的值,所以报错 -- 如果有标识列,一般可以给null值,系统会自动生成 insert into mytable values(null, 'lili', 30, 0) -- 指定需要添加数据的字段 insert into mytable(name, age, gender) values('lili', 30, 0) -- 对于值为null的值,可以不写 insert into mytable(name) values("ok") -- 非空字段需要赋值,否则系统也不会自动为其生成默认值 insert into mytable(age,gender) values(40, 1) -- 虽然可以,但是不建议漏掉非空字段
1.3、修改
语句: update
表名 set
字段1 = 值1, 字段2 = 值2
update mytable set age = age + 1 -- 会把所有的都改变 -- 带条件的写法,只会改变第5条 update mytable set age = age + 10 where id = 5 -- 同样支持 or and not -- 修改多个内容 update mytable set age = age + 1,gender = 1 where id = 5
1.4、删除
不要轻易删除内容!!!!
语法:delete [from]
表名 where
条件 (from可以省略)
-- 删除的操作不能还原,要删除的话,就需要提前备份 delete from mytable where id = 8 -- 同时删除多个 delete from mytable where id in(4,5) delete from mytable where id in(4,5)
小结
==> 查询是返回受影响的内容 ==> 增加、删除和修改是返回受影响的行数
2、常见的函数说明
2.1、总条数 count
-- 查询可以满足条件的记录数 select count(*) from mytable -- 选择符合条件的记录数 select count(id) from mytable -- 如果当前空值,不会对null值进行计算
2.2、最大值、最小值 max min
得到当前的那个值 -- max 获取最大值 min 获取最小值 select max(age) from mytable -- 如果是字符,按照字符的ascll码来排序
2.3、平均值 avg
-- 一般都是数值 select svg(age) from mytable
2.4、排序 order by
select * | 字段列表 form 表列表 where order by asc 升序 desc 降序 select * from mytable -- 降序 select * from mytable order by id desc -- 按照name排序 a-z select * from mytable order by name -- null值会排在前面 -- 实现按照性别,再按照年龄 select * from mytable order by gender,age
2.5、获取指定范围内的数据
limit 获取指定的前n条记录 只有一个参数 -- 前面3条数据 select * from mytable limit 3 -- 后面五条 -- 先做降序,然后再去筛选,并且一定要先排序,再获取,不然会报错 select * from mytable order by id desc limit 5 -- 中间范围的记录 n 偏移量从0开始, m 能够获取的记录数 select * from mytable limit 2,2 -- 第2种写法,和上面的一样 select * from mytable limit 4 offset 2 制作分页 int pageSize = 10; int pagecount = 1; select * from mytable pageSize 4 offset (pagecount - 1) * pageSize
2.6、多表查询
在数据中,防止重复存储数据,所以会把不同的数据放在不同的地方存储 -- 返回初始的数据,没有内部关联的数据 select * from student -- 用户需要的是最终的结果 -- 1.0 采用from where的方式 select * from student,class where student.cid = class.classid where后面的这个 = 表示判断 -- 简写 select studentId,studentName,age,gender,className from student,class where student.cid = class.classid -- 2.0 join 和 inner join都是一样的 on和where的意思也是差不多的 select * from student inner join class on student.cid = class.classid -- left join 如果对应不上的时候,自动让对应的值为空 right join 与之相反 select * from student left join class on student.cid = class.classid left join 是左边的数据会全部显示出来 right join 右边的数据会全部显示出来
3. PHP建立数据库连接
3.1 建立与服务器的连接
mysqli_connect
(设置主机,用户名,密码,想操作的数据库)
这个函数会自动打开连接
如果连接失败,返回false
如果连接成功就返回一个连接对象
<?php // 1.0 设置响应头 header("Content-Type:text/html;chartset=utf-8"); // 2.0 创建数据库连接 $conn = mysqli_connect("localhost", "root", "root", "mybase"); // 3.0 设置编码 // mysqli_set_chartset($conn, "utf8"); mysqli_query($conn, "set name as utf-8"); // 设置编码格式为utf-8 if(!$conn) { die("连接失败"); }
3.2 解决乱码问题
mysqli_set_chartset($conn, "utf8"); //设置数据库编码 header("Content-Type:text/html;chartset=utf-8"); PHP与浏览器编码一致的问题
3.3 PHP操作sql增加、修改和删除
执行sql语句 mysqli_query(连接对象, sql语句) 有返回值
, 成功为true
, 失败为false
// 1.0 新增数据 创建sql语句(删除,修改结合上边数据库的语句 类似的) // 2.0 数据库中字符串必须写在单引号或者双引号中间 $sql = "insert into mytable value(null, '张三', 30, 1)"; $result = mysqli_query($conn, sql); // 3.0 $result 返回类型为bool类型 ,成功为true ,失败为false var_dump($result); // bool(true) | bool(false) if($result) { echo "新增成功"; } else { echo "新增失败<br>"; // 输出具体的报错信息 echo mysqli_error($conn); }
3.4 php查询
因为查询到的是结果集 所以会和增加 删除 修改语句不一样
<?php // 1.0 设置响应头 header("Content-Type:text/html;chartset=utf-8"); // 2.0 创建数据库连接 $conn = mysqli_connect("localhost", "root", "root", "mybase"); // 3.0 设置编码 // mysqli_set_chartset($conn, "utf8"); mysqli_query($conn, "set name as utf-8"); // 设置编码格式为utf-8 if(!$conn) { die("连接失败"); } // 4.0 创建查询sql语句 $sql = "select * from mytable"; // 5.0 执行sql语句 $result = mysqli_query($conn, $sql); var_dump($result); // 获取到大致信息,几行几列,但是并不是具体的数据 ?>
mysqli_num_rows($result)
判断当前结果集中是否有数据
展示结果集中的内容:
mysqli_fetch_array()
:提取数据生成一个数组.同时生成索引数组和关联数组两种形式
mysqli_fetch_assoc()
:提取数据生成一个数组:将数据生成关联数组
mysqli_fetch_row()
:提取数据生成一个数组,将数据生成为索引数组
都只会读取一行数据,但是在读取完毕之后,会将指针指向下一行
读取全部的数据
<?php // 上面的函数都只会读取一行数据,但是在读取完毕之后,会将指针指向下一行 // 如果需要获取多行,则需要重复调用相同的方法,如果没有数据,则返回NULL // !!!!而数据到底有多少,则未可以,所以循环的话并不知道何时停止,while循环就比较适合 while ($arr = mysqli_fetch_array($result, MYSQL_ASSOC);) { $res[] = $arr; } print_r($res); ?>
最终查询代码(完整版)
<?php // 设置响应头 header('content-type:text/html;charset=utf-8'); $conn = mysqli_connect('localhost','root','root','mytable'); // 判断是否连接成功 失败会返回false 成功呢会返回一大串东西 if (!$conn) { die('连接失败'); } // 成功后写SQL语句 // 增加语句 // $sql = "insert into people values(null,'jan','男','2019-1-1','222','12') "; // 修改语句 修改多个的时候where左边逗号右边用or // $sql = "update people set age=18,sex='男' where id = '1' or id='3'"; // 删除 // $sql = "delete from people where id = '4' "; // 查询 $sql = "select * from people"; $result = mysqli_query($conn,$sql); if (!$result) { die ('查询失败'); } else if (mysqli_num_rows($result) == 0) { die ('没有结果'); } else { // 查询成功 while($arr = mysqli_fetch_assoc($result)) { $res[] = $arr; } print_r($res); } ?>
原文地址:https://www.jianshu.com/p/c7e7dd04f368