在此补充一点关于调试php的方法:小程序端返回数据遇到问题时,多通过浏览器输入网址的形式debug,比如把sql命令打出来,放到数据库看结果
(如echo和print_r和return分不清等问题也可以都打印出来看看
比如我就经常忘记在查询字符串时加引号
“浏览器输入网址示例”
http://localhost/test/write.php?open_id=aa&nick_name=d&university=3&hobby=dd&gender=1
个人感觉不用掌握太多php知识,毕竟正式学一门语言还是挺难的(懒)
在网上找一些类似功能的代码完成对应功能就好,有需要时,再找资料就是~
参考这个文章,分段处理连接、读、写部分
大家可以在阅读完链接和本文的部分php代码后,自己设计其他功能⚠️
实现功能:连接数据库
header("Content-type: text/html; charset=utf8");
//1. 声明字符编码
$host='127.0.0.1';//数据库ip
// $host='localhost';//数据库ip
$user='root';//用户名
$password=' ';//密码
$dbName=' ';//要连接的数据库名
$con =new mysqli($host,$user,$password,$dbName,3306);//数据库连接
if ($con->connect_error) {
echo "系统异常,连接数据库失败:", $con->connect_error;
}
// else
// {
// echo "连接成功";
// }
?>
实现功能:以get方式将数据库端数据返回到小程序【好像没用上】
header("Content-type: text/html; charset=utf8");
$my_table='testinfo';
$open_id=$_GET['open_id'];
include 'connect.php';//调用connect.php文件\
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql = "SELECT * FROM $my_table WHERE open_id = '$open_id';";
// mysqli_query($conn, "set names 'utf8'");//不写这句有可能乱码
// echo $sql;
$res=$con->query($sql);
// print_r($res->fetch_all (MYSQLI_BOTH));
if ($res){
$data=$res->fetch_all(PDO::FETCH_LAZY);
echo json_encode($data);//返回二维数组形式的值供小程序端用
}
else{
echo '查询出错!';
}
// fetch_all查询所有行
}
?>
### write.php
**实现功能**:以get方式获取小程序端参数并写入数据库
实现功能:以get方式获取小程序端参数并写入数据库
header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$my_table='testinfo';
$open_id=$_GET['open_id'];
$nick_name=$_GET['nick_name'];
$university=$_GET['university'];
$hobby=$_GET['hobby'];
$gender=$_GET['gender'];
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="INSERT INTO $my_table(open_id, nick_name, university, hobby, gender) VALUES ('$open_id', '$nick_name', $university, '$hobby', $gender);";
// echo $sql; // debug用
$res=$con->query($sql);
// $res = mysqli_query($con, $sql);
if($res){
$arr['status'] = 1;
$arr['info'] = 'success';
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
}
echo json_encode($arr['status']);
// echo json_encode($arr['status']);
// echo json_encode($res);
die;
}
//http://localhost/test/write.php?open_id=aa&nick_name=d&university=3&hobby=dd&gender=1 前端界面debug
?>
实现功能:以get方式获取小程序端参数并将数据库中数据进行更新
header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$my_table='testinfo';
$open_id=$_GET['open_id'];
$nick_name=$_GET['nick_name'];
$university=$_GET['university'];
$hobby=$_GET['hobby'];
$gender=$_GET['gender'];
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql1="DELETE FROM $my_table WHERE open_id = '$open_id';";
$sql2="INSERT INTO $my_table(open_id, nick_name, university, hobby, gender) VALUES ('$open_id', '$nick_name', $university, '$hobby', $gender);";
// echo $sql; // debug用
$res1=$con->query($sql1);
$res2=$con->query($sql2);
// $res = mysqli_query($con, $sql);
if($res1 and $res2){
$arr['status'] = 1;
$arr['info'] = 'success';
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
}
echo json_encode($arr['status']);
// echo json_encode($arr['status']);
// echo json_encode($res);
die;
}
//http://localhost/test/write.php?open_id=aa&nick_name=d&university=3&hobby=dd&gender=1 前端界面debug
?>
实现功能:查询当先open_id在数据库中的状态
header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$my_table='testinfo';
$open_id=$_GET['open_id'];
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="SELECT * FROM $my_table WHERE open_id='$open_id';";
// echo $sql;
$res=$con->query($sql);
// $res = mysqli_query($con, $sql);
if($res){
$arr['status'] = 1;
$arr['info'] = 'success';
if(mysqli_num_rows($res)==0){
$arr['status'] = 1;
$arr['info'] = 'This openid not found';
echo json_encode($arr);
}
if(mysqli_num_rows($res)==1){
$arr['status'] = 2;
$arr['info'] = 'This openid is created';
echo json_encode($arr);
}
if(mysqli_num_rows($res)>1){
$arr['status'] = 3;
$arr['info'] = 'This openid too much';
echo json_encode($arr);
}
// return res;
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
echo json_encode($arr);
}
die;
}
?>
实现功能:删除多余数据
header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$my_table='testinfo';
$open_id=$_GET['open_id'];
$nick_name=$_GET['nick_name'];
$university=$_GET['university'];
$hobby=$_GET['hobby'];
$gender=$_GET['gender'];
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="DELETE FROM $my_table WHERE open_id = '$open_id';";
// echo $sql; // debug用
$res=$con->query($sql);
// $res = mysqli_query($con, $sql);
if($res){
$arr['status'] = 1;
$arr['info'] = 'success';
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
}
echo json_encode($arr['status']);
die;
}
//http://localhost/test/write.php?open_id=aa&nick_name=d&university=3&hobby=dd&gender=1 前端界面debug
?>
记得放到正确的路径上✔