微信小程序wx.request(8) 实现增、删、改、查(后端为php版本)

微信小程序是一个集js动态效果,html展示,css样式,以及json格式参数为一体的强大的微信前段应用,分别对应的是wxml,wxss,js,json功能页面,其中如何与传统数据库进行参数传递和处理,是微信小程序的核心开发之一

接下来,我们将就使用wx.request对数据库进行,增添数据,删除数据,更改数据,查找数据四大和数据库交互的基本功能进行分析和展示。

php用于写接口,简单又快捷方便,所以这里,我们使用php为数据库接口工具。
一、增添数据

我们假定云服务器为http://180.200.24.1:8080,接口为add.php,使用腾讯云数据库,内网地址为:172.15.0.2:3306,数据库结构如下:

   数据库的名字为testinfo,数据表为datatest

add.php如下

$id = $_GET["id"];
$username = $_GET["username"];
$password = $_GET["password"];

$conn = mysqli_connect("172.15.0.2", "root","*****","testinfo");//连接MYSQL数据库
if(!$conn)
{
    die('Could not connect:' .mysqli_error());
}
$sql = "INSERT INTO datatest (id,username,password) VALUES('{$id}','{$username}','{$password}')";
$result = mysqli_query($conn, $sql);

小程序端:

index.js中
wx.request({
    url:'http://180.200.24.1:8080/add.php',
    data:{
        id:1,
        username:'jiangliu',
        password:'000'
    },
    header:{
        'Content-Type':'application/json'
    },
    method:'GET',
    success: function(res){
      console.log(res)
    }
})

二、删除数据
我们假定云服务器为http://180.200.24.1:8080,接口为delete.php,使用腾讯云数据库,内网地址为:172.15.0.2:3306,数据库结构如下:

   数据库的名字为testinfo,数据表为datatest

php版本
delete.php如下:

  
$name=$_GET["username"] ;//接收参数
$conn = mysqli_connect("172.15.0.2", "root","*****","testinfo");//连接MYSQL数据库
if(!$conn)
{
    die('Could not connect:' .mysqli_error());
}
$sql = "delete from datatest where username='{$name}'";
$result = mysqli_query($conn, $sql);

小程序端:

index.js

wx.request({
   url:'http://180.200.24.1:8080/delete.php',
   data:{
    username:'zhansan',
   },
   method:'GET',
   success: function(res){
      console.log(res)
    }
})

**三、更改数据:**
我们假定云服务器为http://180.200.24.1:8080,接口为edit.php,使用腾讯云数据库,内网地址为:172.15.0.2:3306,数据库结构如下:

    

       数据库的名字为testinfo,数据表为datatest

接口edit.php如下:

   

```php
$id = $_GET["id"];
$username = $_GET["username"];
$password = $_GET["password"];
$conn = mysqli_connect("172.15.0.2", "root","******","testinfo");//连接MYSQL数据库
if(!$conn)
{
    die('Could not connect:' .mysql_error());
}
$sql = "update datatest set username='{$username}',password='{$password}' where id='{$id}'";
$result = mysqli_query($conn, $sql);

小程序端:

index.js

wx.request({
  url:'http://180.200.24.1:8080/edit.php',
  data:{
      id:2,
      username: 'kksasss',
      password: 921,
   },
   method:'GET',
   success: function(res){
      console.log(res)
    } 
})
**四、查询数据**
我们假定云服务器为http://180.200.24.1:8080,接口为search.php,使用腾讯云数据库,内网地址为:172.15.0.2:3306,数据库结构如下:

    

       数据库的名字为testinfo,数据表为datatest

search.php

```php
$name=$_GET['username'] ;//接收参数
$conn = mysqli_connect("172.15.0.2", "root","*******","testinfo");//连接MYSQL数据库
$sql = "SELECT username,password FROM datatest WHERE username='{$name}'";//响应请求
$result = mysqli_query($conn, $sql);//执行
if (mysqli_num_rows($result) > 0) {// 输出小程序数组
    while($row = mysqli_fetch_assoc($result)) {
        echo json_encode($row);//将请求结果转换为json格式
    }
}

小程序端:

index.js

wx.request({
   url:'http://180.200.24.1:8080/search.php'
   data:{
      username:'zhansan'
   },
   header:{
       'content-type':'application/json'
   },
   method:'GET',
   success:function(res){
          console.log(res.data)
   },
})



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值