通过php文件进行数据中转。经试验有效可行!!!
1、php 部分
1.1、连接数据库
<?php
//header("Content-type: text/html; charset=utf8");
//1. 声明字符编码
$host='127.0.0.1';//数据库ip
$user='root';//用户名
$password='123456';//密码
$dbName='mysql';//要连接的数据库名
$con =new mysqli($host,$user,$password,$dbName,3308);//数据库连接
/*if ($con->connect_error) {
echo "系统异常,连接数据库失败";
}
else
{
echo "连接成功";
}*/
?>
1.2、读取php
<?php
//header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$something=$_GET['something'];//'小明';//接收小程序传过来的参数'1';//
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="SELECT * FROM `1students` WHERE `name` = '$something' ";//根据传入的参数查询数据库中的数据
$res=$con->query($sql);
echo $con->error;
print_r($res->fetch_all (MYSQLI_BOTH));
if ($res){
$data=$res->fetch_all(PDO::FETCH_LAZY);
}
else{
echo '查询出错!';
}
//fetch_all查询所有行
echo json_encode($data);//返回二维数组形式的值供小程序端用
}
?>
1.3、写入php
<?php
//header("Content-type: text/html; charset=utf8");
include 'connect.php';//调用connect.php文件
$something1=$_GET['something1'];//'小明';//
$something2=$_GET['something2'];//'16';//
$something3=$_GET['something3'];//'0';//
if ($con->connect_error) {
die("连接失败:".$con->connect_error);
}
else
{
$sql="INSERT INTO `1students`(`name`, `age`, `xb`) VALUES ('$something1','$something2','$something3');";
$res=$con->query($sql);
if($res){
$arr['status'] = 1;
$arr['info'] = 'success';
}else{
$arr['status'] = 0;
$arr['info'] = 'error';
}
echo json_encode($arr);
die;
}
?>
2、微信小程序部分
直接贴代码:
2.1、js部分:
onLoad: function() {
var that = this;
//读取数据库
wx.request({
url: 'http://127.0.0.1/get.php',
method: 'GET',
data: {
something: '1'
},
header: {
'content-Type': 'application/json'
},
success(res) {
console.log(res),
that.setData({
postList: res.data,
});
}
});
//写入数据库
wx.request({
url: 'http://127.0.0.1/post.php',
method: 'GET',
data: {
something1: '小王',
something2: '16',
something3: '0'
},
header: {
'content-Type': 'application/x-www-form-urlencoded'
},
success(res) {
console.log(res.data);
if (res.data.status == 0) {
wx.showToast({
title: '提交失败!!!',
icon: 'loading',
duration: 1500
})
} else {
wx.showToast({
title: '提交成功!!!', //这里打印出登录成功
icon: 'success',
duration: 1000
})
}
}
});
},
2.2、wxml部分
<text>{{postList}}</text>
2.3、app.json文件中添加
"pages": [
"pages/index/index",
"pages/logs/logs",
"pages/sql/sql" //对应的位置
],