微信小程序连接MySQL数据库(读取+写入)demo

通过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"    //对应的位置
  ],

 

 

  • 34
    点赞
  • 415
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大臉喵愛吃魚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值