PHP增、删、改、查及连接数据库

增删改查:

增加:insert into 表名 (字段1,字段2,字段3...) value/values ('值1','值2','值3'....)

删除:delete from 表名 where 条件

修改:update 表名 set 字段1='值1',字段2='值2'... where 条件

查询:select *(字段名 as 别名) from 表名 as 别名 【where 字段1='值1'/字段1 like '%值1%' and(or) 字段2='值2'/字段2 like '%值2%'】【 order by 字段 asc/desc】【 limit 偏移量,显示条数】
字段:* ,具体的字段名,count(*),sum(字段名),avg(字段名)
1、select * from 表名
2、select * from 表名 where 条件
3、select * from 表名 order by 字段 asc/desc
4、select * from 表名 limit 偏移量,显示条数
5、select * from 表名 where 条件 limit 偏移量,显示条数
6、select * from 表名 where 条件 order by 字段 asc/desc
7、select * from 表名 order by 字段 asc/desc limit 偏移量,显示条数
8、select count(*) from 表名
9、select count(*) from 表名 where 条件
10、select count(*) from 表名 order by 字段 asc/desc
11、select count(*) from 表名 limit 偏移量,显示条数
12、select count(*) from 表名 where 条件 limit 偏移量,显示条数
13、select count(*) from 表名 where 条件 order by 字段 asc/desc
14、select count(*) from 表名 order by 字段 asc/desc limit 偏移量,显示条数


项目里编码格式
1、php编码格式 header("content-type:text/html;charset=utf-8");
2、编辑器编码格式 utf-8
3、数据库、数据表的编码格式 utf-8
4、php执行数据库的sql语句操作,php执行设置数据库编码格式
5、前端页面 utf-8


1、链接数据库的函数
连接数据库的资源对象 = mysqli_connect('ip地址','用户名','密码'[,'数据库名'])
2、选择数据库
mysqli_select_db('连接数据库的资源对象','数据库名');
3、设置编码格式
mysqli_query('连接数据库的资源对象','set names utf8')
4、编写sql语句

5、执行sql语句
mysqli_query('连接数据库的资源对象','编码SQL语句')

6、查询sql(取出结果)
mysqli_fetch_assoc(): 一次只能取一条数组(结果)
mysqli_fetch_array():一次只能取一条数组(结果)

取多条数据
$data = [];
while($row = mysqli_fetch_assoc($query)){
$data[] = $row1;
}


PHP阻止代码运行 exit die
1、echo '连接失败';die;
2、die('连接失败');
3、echo '连接失败';exit;

猜你喜欢

转载自www.cnblogs.com/LH1994-php/p/9806000.html