在php中提供了关于连接MySQL以及处理操作结果集的封装函数
常用方法:
1.连接数据库方法
mysqli_connect(string $host = '', string $user = '', string $password = '', string $database = '', string $port = '', string $socket = '')
返回值:返回连接MySQL服务器的连接对象
参数一:$host:主机名
参数二:$user:数据库用户名
参数三:$password:数据库密码
参数四:$database:数据库名称
参数五:$port:服务端口
2.选择数据库方法:
mysqli_select_db(\mysqli $link, string $dbname)
参数一:mysqli_connect方法连接返回的mysqli类型的$link
参数二:数据库名
3.设置数据库字符编码
mysqli_set_charset(\mysqli $link, string $charset)
参数一:mysqli_connect方法连接返回的mysqli类型的$link
参数二:自定义的MySQL编码常见的有(utf8,gbk,utf8mb4)
4.执行SQL语句
mysqli_query(\mysqli $link, string $query, int $resultmode = MYSQLI_STORE_RESULT)
参数一:mysqli_connect方法连接返回的mysqli类型的$link
参数二:自定义的SQL语句
参数三:可忽略,后详写
返回值:返回执行结果
4.获取结果集行数
mysqli_num_rows(\mysqli_result $result)
参数一:mysqli_query的执行结果$result
返回值:返回结果集中行数
5.获取所有结果集并以指定参数形式返回
mysqli_fetch_all(\mysqli_result $result, int $resulttype = MYSQLI_NUM)
参数一:mysqli_query的执行结果$result
参数二:这个可选参数是一个常量,指示应该从当前行数据生成哪种类型的数组。这个参数的可能值是常量MYSQLI_ASSOC、MYSQLI_NUM或MYSQLI_BOTH。
6.释放结果集对象的内存
mysqli_free_result(\mysqli_result $result)
参数一:mysqli_query的执行结果$result
以上方法是php操作MySQL增删改查中常用的方法
动一动
简易的MySQL增删改查案例
1.公共配置文件 conn.php
<?php
//开始配置数据库信息
$host = "localhost";
$port = "3306";
$user = "root";
$pass = "root";
$dbname = "stu_info";
$charset ="utf8mb4";
//连接数据库
if (!$link = mysqli_connect($host,$user,$pass,$dbname,$port)) {
die();
}
//选择数据库
if(!mysqli_select_db($link,$dbname)){
die();
}
//设置字符编码
mysqli_set_charset($link,$charset);
$sql = "select * from stu_info";
$resoultall = mysqli_query($link,$sql);
?>
2.列表展示页 list.php文件
<?php
//导入模块
require_once('./coon.php');
$stu_num = mysqli_num_rows($resoultall);
$arrs = mysqli_fetch_all($resoultall,MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>学生列表中心</title>
</head>
<script>
function checkdelete(id){
if(window.confirm("确认删除?")){
location.href="delete.php?id="+id;
}
}
</script>
<body>
<h2 align ="center">学生信息列表中心</h2>
<table align="center" border ="1" rules="all" width="400">
<tr>
<caption>共有<?php echo $stu_num; ?>条记录</caption>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>电话</th>
<th>操作项</th>
</tr>
<?php
foreach ($arrs as $arr) {
?>
<tr>
<td><?php echo $arr['id'] ?></td>
<td><?php echo $arr['name'] ?></td>
<td><?php echo $arr['tel'] ?></td>
<td align = "center">
<a href="add.php">添加</a>
<a href="change.php?id=<?php echo $arr['id'] ?>">修改</a>
<a href ="#" onclick="checkdelete(<?php echo $arr['id'] ;?>);">删除</a>
</td>
<?php } ?>
</tr>
</table>
</body>
</html>
3.增加学生信息
<?php
require_once('./coon.php');
if(isset($_GET['name'])) {
$name=$_GET['name'];
$tel=$_GET['tel'];
$sql2 = "insert into stu_info values(null,'".$name."','".$tel."')";
if(mysqli_query($link,$sql2)){
echo "添加成功!";
header("refresh:0.5;url=./list.php");
}else{
echo "请检查添加字段!";
header("refresh:0.5;url=./list.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>添加学生信息</title>
</head>
<body>
<table align="center" border ="1" rules="all" width="400">
<tr>
</tr>
<tr>
<th>姓名</th>
<th>电话</th>
</tr>
<tr>
<td><input type="text" id="name"></td>
<td><input type="text" id="tel"></td>
<td align = "center">
<input type="button" value="添加" id="submit">
</td>
</tr>
</table>
<script>
var submit= document.getElementById('submit');
submit.onclick=function(){
var name= document.getElementById('name').value;
var tel= document.getElementById('tel').value;
location.href=`./add.php?name=${name}&tel=${tel}`;
}
</script>
</body>
</html>
4.修改学生信息
<?php
require_once('./coon.php');
//获取需要修改的学生的id
$id = $_GET["id"];
$sql1 = "select * from stu_info where id = $id ";
$resoult = mysqli_query($link,$sql1);
$res = mysqli_fetch_all($resoult,MYSQLI_ASSOC);
if(isset($_GET['name'])) {
$idd=$_GET['id'];
$name=$_GET['name'];
$tel=$_GET['tel'];
$sql2 = "update stu_info set `id`= $idd,`name`= '".$name."', `tel`='".$tel."' where id = $id ";
if(mysqli_query($link,$sql2)){
header("refresh:0.1;url=./list.php");
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>修改学生信息</title>
</head>
<body>
<table align="center" border ="1" rules="all" width="400">
<tr>
</tr>
<tr>
<th>编号</th>
<th>姓名</th>
<th>电话</th>
<th>操作项</th>
</tr>
<?php
foreach ($res as $arr) {
?>
<tr>
<td><input type="text" value="<?php echo $arr['id'] ?>" id="id"></td>
<td><input type="text" value="<?php echo $arr['name'] ?>" id="name"></td>
<td><input type="text" value="<?php echo $arr['tel'] ?>" id="tel"></td>
<td align = "center">
<input type="button" value="保存" id="submit">
</td>
<?php } ?>
</tr>
</table>
</body>
<script>
var submit= document.getElementById('submit');
console.log(submit);
submit.onclick=function(){
var id= document.getElementById('id').value;
var name= document.getElementById('name').value;
var tel= document.getElementById('tel').value;
location.href=`./change.php?id=${id}&name=${name}&tel=${tel}`;
}
</script>
</html>
5.删除学生信息
<?php
//导入mysql模块
require_once('./coon.php');
//获取id的属性
$id = $_GET["id"];
$sql1 = "delete from `stu_info` where id = $id";
$sql2 = "alter table `stu_info` auto_increment =$id";
$sql3 = "UPDATE stu_info SET `id` = id-1 WHERE id>$id";
$sql4= "alter table `stu_info` auto_increment =1";
$arrs = mysqli_fetch_all($resoultall,MYSQLI_ASSOC);
if (count($arrs)!=0) {
if(mysqli_query($link,$sql1)&&mysqli_query($link,$sql2)&&mysqli_query($link,$sql3)){
?>
<h2>删除成功!</h2>
<?php
header("refresh:1;url=./list.php");
die(); //中止程序向下运行
}else{
mysqli_query($link,$sql4);
}
}
?>
六:总结
这下知道为什么一些培训机构在院校培训前端会用PHP了
来源:https://www.cnblogs.com/bai-boy/p/12199506.html