微信小程序連接MySQL數(shù)據(jù)庫(kù)的全過(guò)程
簡(jiǎn)要說(shuō)明:
承接上一個(gè)商品列表頁(yè),在服務(wù)器端連接MySQL數(shù)據(jù)庫(kù),通過(guò)條件匹配查尋數(shù)據(jù)并顯示在客戶(hù)端
準(zhǔn)備工作
1、node.js
2、微信開(kāi)發(fā)者工具
3、MySQL數(shù)據(jù)庫(kù)
MySQL配置數(shù)據(jù)庫(kù)、數(shù)據(jù)表
通過(guò)可視化的Workbench,可以很容易的建立自己的數(shù)據(jù)庫(kù)、數(shù)據(jù)表。這里直接截個(gè)圖就好了

推薦一個(gè)工具 Navicat for MySQL,以后可以通過(guò)它連接自己的數(shù)據(jù)庫(kù)

目錄結(jié)構(gòu)

客戶(hù)端代碼實(shí)現(xiàn)
index.wxml (變化不大,加了跳轉(zhuǎn)按鈕)
<view class="contain">
<form bindsubmit="submit">
<view>
<text id="top">商品</text>
<text id="r" bindtap="jump">了解更多</text>
<!-- <button type="default" bindtap="jump">了解更多</button> -->
<!-- <button>詳情</button> -->
<checkbox-group name="skills">
<label wx:for="{{skill}}" wx:key="value">
<checkbox value="{{item.value}}" checked="{{item.checked}}">
<!-- {{item.name}} -->
<image id="img" src="../image/{{item.name}}.jpg"></image>
<view><text>{{item.text}}{{}}</text></view>
</checkbox>
</label>
</checkbox-group>
</view>
<button form-type="submit">提交</button>
<text id="sum">合計(jì):{{result}}</text>
</form>
</view>
index.wxss
/* pages/index/index.wxss */
.contain{
/* background-color: aqua; */
margin: 15px auto;
}
#top{
/* margin:0 auto; */
margin-left: 20px;
}
#r{
margin-left: 150px;
}
#img{
/* float: left; */
width: 120px;
height: 120px;
}
label{
height: 150px;
position: relative;
display: block;
margin-left: 20px;
}
label view{
position: absolute;
display: inline;
padding-right: 20px;
padding-top: 50px;
}
#sum{
margin-left: 20px;
}
index.js (變化不大,加了跳轉(zhuǎn)函數(shù))
// pages/index/index.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
skill: [
{name:'01',value:600,checked:false,text:'宇智波佐助\n價(jià)格: 600.00'},
{name:'02',value:300,checked:false,text:'宇智波鼬\n價(jià)格: 300.00'},
{name:'03',value:500,checked:false,text:'旗木卡卡西\n價(jià)格: 500.00'},
{name:'04',value:700,checked:false,text:'路飛、紅發(fā)香克斯\n價(jià)格: 700.00'},
{name:'07',value:350,checked:true,text:'索隆\n價(jià)格: 350.00'},
{name:'08',value:799,checked:true,text:'路飛\n價(jià)格: 799.00'},
],
result:[],
names:[]
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
var that =this
wx.request({
url: 'http://127.0.0.1:3000/',
success:function(res){
// console.log(res.data)
that.setData({names:res.data})
}
})
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶(hù)下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶(hù)點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
},
submit:function(e){
var that=this
wx.request({
method:'POST',
url: 'http://127.0.0.1:3000',
data:e.detail.value,
success:function (res){
const a=res.data.skills
console.log(a)
//求和計(jì)算
const reducer=(accumlator,currentValue)=>parseInt(accumlator)+parseInt(currentValue)
console.log(a.reduce(reducer))
const sum=a.reduce(reducer)
that.setData({result:sum})
}
})
},
jump:function(){
wx.navigateTo({
url: '../about/about',
})
}
})
index.json (未做修改)
about.wxml
<!--pages/about/about.wxml-->
<view>
<view id="look">
<text>查看一下他們的詳細(xì)資料吧!</text>
</view>
<form bindsubmit="submit">
<view class="select">
<input id="input" name="name" type="text" value="路飛"/>
<button form-type="submit" id="btn">搜索</button>
</view>
</form>
<view id="result">
<text>搜索結(jié)果:</text>
<textarea name="" id="out" cols="30" rows="10">{{text[0].detail}}</textarea>
</view>
<button id="bottom" bindtap="back">返回</button>
</view>
about.wxss
/* pages/about/about.wxss */
#look{
margin-top: 20px;
margin-bottom: 20px;
}
#input{
border: 1px solid gray;
}
#btn{
margin-top: 10px;
}
#out{
border: 1px solid gray;
}
#bottom{
margin-top: 50px;
}
#result{
margin-top: 20px;
}
about.js
// pages/about/about.js
Page({
/**
* 頁(yè)面的初始數(shù)據(jù)
*/
data: {
text:{}
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
*/
onLoad: function (options) {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面顯示
*/
onShow: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面隱藏
*/
onHide: function () {
},
/**
* 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面卸載
*/
onUnload: function () {
},
/**
* 頁(yè)面相關(guān)事件處理函數(shù)--監(jiān)聽(tīng)用戶(hù)下拉動(dòng)作
*/
onPullDownRefresh: function () {
},
/**
* 頁(yè)面上拉觸底事件的處理函數(shù)
*/
onReachBottom: function () {
},
/**
* 用戶(hù)點(diǎn)擊右上角分享
*/
onShareAppMessage: function () {
},
back:function(){
wx.navigateBack()
},
//提交
submit:function(e){
var that=this
wx.request({
method:'POST',
data:e.detail.value,
url: 'http://127.0.0.1:3000/show',
success:function(res){
// console.log(res.data)
that.setData({text:res.data})
}
})
}
})
about.json
{
"navigationBarBackgroundColor":"#fff",
"navigationBarTitleText":"詳情",
"navigationBarTextStyle":"black",
"usingComponents": {}
}
服務(wù)器端代碼實(shí)現(xiàn)
server.js
const express=require('express')
const bodyParser =require('body-parser')
const app=express()
const mysql = require('mysql')
app.use(bodyParser.json())
//處理post請(qǐng)求
app.post('/',(req,res) => {
console.log(req.body)
res.json(req.body)
})
app.post('/show',(req,res)=>{
console.log(req.body.name)
const a=req.body.name
var connection=mysql.createConnection({
host:'localhost',
user:'你的用戶(hù)名',
password:'你的密碼',
database:'數(shù)據(jù)庫(kù)名字'
})
connection.connect();
connection.query("select detail from price where name='"+a+"'",function(error,results,fields){
if(error) throw console.error;
res.json(results)
console.log(results)
})
connection.end();
})
app.get('/',(req,res)=>{
var connection = mysql.createConnection({
host:'localhost',
user:'你的用戶(hù)名',
password:'你的密碼',
database:'數(shù)據(jù)庫(kù)名字'
});
connection.connect();
//查找所有的人物名字返回給客戶(hù)端。其實(shí)沒(méi)必要(測(cè)試用的)
connection.query('select name from price',function(error,results,fields){
if(error) throw error;
res.json(results)
// console.log(results)
})
connection.end();
})
app.listen(3000,()=>{
console.log('server running at http://127.0.0.1:3000')
})
效果展示
主界面

跳轉(zhuǎn)界面

界面有點(diǎn)丑,慢慢優(yōu)化
總結(jié)
到此這篇關(guān)于微信小程序連接MySQL數(shù)據(jù)庫(kù)的文章就介紹到這了,更多相關(guān)微信小程序連接MySQL內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解讀input標(biāo)簽的value屬性及name屬性
這篇文章主要介紹了解讀input標(biāo)簽的value屬性,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
淺談redux, koa, express 中間件實(shí)現(xiàn)對(duì)比解析
這篇文章主要介紹了淺談redux, koa, express 中間件實(shí)現(xiàn)對(duì)比解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
javascript css紅色經(jīng)典選項(xiàng)卡效果實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了javascript css紅色經(jīng)典選項(xiàng)卡效果的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-05-05
基于javascript實(shí)現(xiàn)動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間
這篇文章主要介紹了基于javascript實(shí)現(xiàn)動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間,以一個(gè)完整實(shí)例形式較為詳細(xì)的分析了js動(dòng)態(tài)顯示當(dāng)前系統(tǒng)時(shí)間的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
微信小程序 云開(kāi)發(fā)模糊查詢(xún)實(shí)現(xiàn)解析
這篇文章主要介紹了微信小程序 云開(kāi)發(fā)模糊查詢(xún)實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
詳解微信小程序開(kāi)發(fā)之formId使用(模板消息)
這篇文章主要介紹了詳解微信小程序開(kāi)發(fā)之formId使用(模板消息),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08

