<?php
编码
header("content-type:text/html;charset=utf8");
连接数据库
$conn=new mysqli(‘localhost‘,‘root‘,‘root‘,‘服务器名字‘,‘3306‘);
判断成功否
if($conn->connect_error){
die(‘连接失败‘.$conn->connect_error);
}
读库编码
$conn->query(‘set character set utf8‘);
写库编码
$conn->query(‘set name utf8‘);
上面代码写入一个public.php文件里,用include "public.php";引入
接收客户端信息
$id=$_GET(‘id); (id一般直接放url后面,通过get直接请求到)
$uname=$_REQUEST(‘username‘);
$upwd=$_REQUEST(‘password‘);
编写sql语句
$sql="insert into `表名` (字段id,字段1,字段2) values (‘$id‘,‘$uname‘,‘$upwd‘)";
$sql="delete form `表名` where 字段id=‘$id";
$sql="update `表名` set 字段id=‘$id‘,字段1=‘$uname‘,字段2=‘$upwd‘ where 字段id=‘ $id‘";
$sql="select * from `表名` where 字段id=‘$id‘";
执行sql语句,曾、删、改返回值都是受影响的行数(值为0,1,2,3...);查询返回值为结果集(值类似是表中所有数据的一个数组对象,只是类似)
曾,删,改操作一样
$res=$conn->query($sql);
if($res){
echo "<script>alert(‘注册成功‘);location.href=‘成功的页面‘;</script>";
}else{
echo "<script>alert(‘注册失败‘);location.href=‘失败的页面‘;</script>";
}
所以查询还要多一步,取得里面的数据
可以先查看结果集里有多少条数据,来判断用户名存在否
$n=mysqli_num_row($res)
if(!$n){
echo "<script>alert(‘用户名不存在‘);location.href=‘失败的页面‘;</script>";
}else{
只能取得表中一行数据,该数据为一行表格数据的数组对象
$row=$res->fetch_assoc();
if($row["字段1"]==$uname && $row["字段2"]==$upwd){
echo "<script>alert(‘登入成功‘);location.href=‘成功的页面‘;</script>";
}else{
echo "<script>alert(‘密码错误‘);location.href=‘失败的页面‘;</script>";
}
}
取得表中每行数据,该数据为表格所有数据的数组对象
$row=$res->fetch_all();
原文:https://www.cnblogs.com/xin1021/p/9328220.html