From 614d65c3b76dad6a5d3fe23dacb34400e59eb6f3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A2=81=E6=B5=A9?= <1599724449@qq.com>
Date: Mon, 23 May 2022 14:49:10 +0800
Subject: [PATCH] php
---
...02\345\270\270\345\244\204\347\220\206.md" | 4 -
"\346\242\201\346\265\251/20220523-mysql.md" | 194 ++++++++++++++++++
2 files changed, 194 insertions(+), 4 deletions(-)
create mode 100644 "\346\242\201\346\265\251/20220523-mysql.md"
diff --git "a/\346\242\201\346\265\251/20220518-\346\226\207\344\273\266\344\270\212\344\274\240\345\222\214\345\274\202\345\270\270\345\244\204\347\220\206.md" "b/\346\242\201\346\265\251/20220518-\346\226\207\344\273\266\344\270\212\344\274\240\345\222\214\345\274\202\345\270\270\345\244\204\347\220\206.md"
index 234e2b0..788c8dd 100644
--- "a/\346\242\201\346\265\251/20220518-\346\226\207\344\273\266\344\270\212\344\274\240\345\222\214\345\274\202\345\270\270\345\244\204\347\220\206.md"
+++ "b/\346\242\201\346\265\251/20220518-\346\226\207\344\273\266\344\270\212\344\274\240\345\222\214\345\274\202\345\270\270\345\244\204\347\220\206.md"
@@ -222,11 +222,7 @@ $_COOKIE-> 用于绘画控制
$_ENY-> 服务器端环境变量
-**调错**
-显示错误代码
-
-$_ERROR()
# 作业
diff --git "a/\346\242\201\346\265\251/20220523-mysql.md" "b/\346\242\201\346\265\251/20220523-mysql.md"
new file mode 100644
index 0000000..f1ed0c6
--- /dev/null
+++ "b/\346\242\201\346\265\251/20220523-mysql.md"
@@ -0,0 +1,194 @@
+# PHP操作MySQL
+
+### 1、掌握php访问数据库的基本步骤
+
+**连接数据库服务器**
+
+**选择数据库**
+
+ 连接mysql服务器同时选择数据库
+
+ mysqli_connect(host服务器地址,username用户名,password密码,dbname数据库名称,port端口,socket)
+
+```php
+$conn = mysqli_connect('localhost','lh','123456','student');
+//变量未知,用var_dump来查看,
+var_dump($conn);
+//或者用if返回对象或错误
+if($conn){
+ echo "连接成功";
+}else{
+ die("连接失败,错误".mysqli_connect_error());
+}
+```
+
+
+
+ 连接mysql服务器,再连接数据库
+
+ mysqli_connect(host服务器地址,username用户名,password密码)
+
+ mysqli_select_db($link,db_name)
+
+```php
+error_reporting(E_ALL ^ E_WARNING);//^表示排除,隐藏所有错误,除了warning警告
+$a = mysqli_connect('localhost','lh','123456');
+if(!$a){
+ die("连接失败,错误".mysqli_connect_error());
+}
+$b = mysqli_select_db($a,'student');
+if(!$b){
+ die("连接数据库失败,错误".mysqli_error($a));
+}
+echo "数据库连接成功";
+```
+
+
+
+## 2、掌握MySQL扩展函数
+
+**执行sql语句**
+
+增,删,改,查
+
+$mysqli_query()
+
+```mysql
+增
+insert into `student` (`name`) values('值');
+删
+delete from `student` where `id`=2;
+改
+update `studnet` set `name` = '值' where <条件>;
+查
+select * from `student`;
+```
+
+
+
+```php
+增
+$sql = "insert into `student<表名>`(`name<字段名>`) values('值')";
+//$result = mysqli_query($conn,$sql);
+//if($result){
+// echo "添加成功";
+//}else{
+// echo "添加失败";
+//}
+删
+$sql ="delete from `student` where `id`=2";
+//$result = mysqli_query($a,$sql) or die("执行错误".mysqli_error($a));
+//if($result){
+// echo "删除成功";
+//}else{
+// echo "删除失败";
+//}
+改
+$sql = "update `student` set `name` ='值' where <条件>";
+
+```
+
+```php
+
+
+$num = mysqli_affected_rows($a);//返回最近一次query操作受影响的行数,0表示没有受影响的行数,-1表示返回错误,>0表示受影响的行数
+
+if($num==0){
+
+echo "没有对应数据";
+
+}else if($num<0){
+
+die("程序失败");
+
+}else{
+ echo "成功,删除了".$num."行";
+}
+
+
+```
+
+**处理结果集**
+
+```php
+
+查
+$sql = "select * from `student`";
+$result = mysqli_query($a,$sql) or die("执行错误".mysqli_error($a));
+//返回一个二维数组
+$all_res =mysqli_fetch_all($result);
+//返回一行数组,包含索引和关联数组,每次之显示一行,要显示多行要用循环
+$all_res =mysqli_fetch_array($result);
+//以关联数组为形式返回一行,每次自动下一行
+$all_res =mysqli_fetch_assoc($result);
+//以索引数组为形式返回一行,每次自动下一行
+$all_res =mysqli_fetch_row($result);
+//以对象为形式返回一行,每次自动下一行
+$all_res =mysqli_fetch_object($result);
+
+```
+
+
+
+**关闭连接**
+
+```php
+mysqli_close($conn);
+```
+
+
+
+
+
+# 作业
+
+```mysql
+create database studentt charset utf8;
+use studentt;
+create table user(
+ id int primary key auto_increment,
+ name varchar(20) not null,
+ score decimal not null
+);
+```
+
+```php
+";
+$b = mysqli_select_db($a,'studentt');
+if(!$b){
+ die("连接数据库失败,错误".mysqli_error($a));
+}
+echo "数据库连接成功";
+echo "
";
+mysqli_query($a,"set names utf8");
+//增加
+$sql_zj = "insert into `user`(`name`,`score`) values('陈小龙','98.5'),('吴大虫','75.5'),('陈得胜','66')";
+$res_zj = mysqli_query($a,$sql_zj) or die("添加数据失败!错误为:".mysqli_error($a));
+
+//删除
+$sql_sc = "delete from `user` where `id`>3";
+$res_sc = mysqli_query($a,$sql_sc) or die("删除数据失败!错误为:".mysqli_error($a));
+//修改
+$sql_xg = "update `user` set `name` = '陈胜' where `id`=3";
+$res_xg = mysqli_query($a,$sql_xg) or die("修改数据失败!错误为:".mysqli_error($a));
+//查询
+$sql_cx = "select * from `user`";
+$res_cx = mysqli_query($a,$sql_cx) or die("执行错误".mysqli_error($a));
+$all_res =mysqli_fetch_assoc($res_cx);
+//打印结果
+while($c=mysqli_fetch_assoc($res_cx)){
+ echo $c['id']."|".$c['name']."|".$c['score']."
";
+}
+```
+
+
+
--
Gitee