微信小程序怎么连接MySQL数据库
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇"微信小程序怎么连接MySQL数据库"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇
千家信息网最后更新 2025年11月08日微信小程序怎么连接MySQL数据库
这篇"微信小程序怎么连接MySQL数据库"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"微信小程序怎么连接MySQL数据库"文章吧。
准备工作
1、node.js
2、微信开发者工具
3、MySQL数据库
MySQL配置数据库、数据表
通过可视化的Workbench,可以很容易的建立自己的数据库、数据表。这里直接截个图就好了
推荐一个工具 Navicat for MySQL,以后可以通过它连接自己的数据库
目录结构
客户端代码实现
index.wxml (变化不大,加了跳转按钮)
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 (变化不大,加了跳转函数)
// pages/index/index.jsPage({ /** * 页面的初始数据 */ data: { skill: [ {name:'01',value:600,checked:false,text:'宇智波佐助\n价格: 600.00'}, {name:'02',value:300,checked:false,text:'宇智波鼬\n价格: 300.00'}, {name:'03',value:500,checked:false,text:'旗木卡卡西\n价格: 500.00'}, {name:'04',value:700,checked:false,text:'路飞、红发香克斯\n价格: 700.00'}, {name:'07',value:350,checked:true,text:'索隆\n价格: 350.00'}, {name:'08',value:799,checked:true,text:'路飞\n价格: 799.00'}, ], result:[], names:[] }, /** * 生命周期函数--监听页面加载 */ 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}) } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ 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) //求和计算 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
查看一下他们的详细资料吧! 搜索结果:
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.jsPage({ /** * 页面的初始数据 */ data: { text:{} }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ 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": {}}服务器端代码实现
server.js
const express=require('express')const bodyParser =require('body-parser')const app=express()const mysql = require('mysql')app.use(bodyParser.json())//处理post请求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:'你的用户名', password:'你的密码', database:'数据库名字' }) 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:'你的用户名', password:'你的密码', database:'数据库名字' }); connection.connect(); //查找所有的人物名字返回给客户端。其实没必要(测试用的) 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')})效果展示
主界面

跳转界面
以上就是关于"微信小程序怎么连接MySQL数据库"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
页面
函数
数据
监听
周期
周期函数
生命
数据库
价格
用户
内容
处理
事件
程序
名字
更多
不大
代码
动作
客户
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
数据库应用层次哪3种类型
局域网用的数据库
孤岛惊魂4连不上服务器怎么玩
宣传网络安全相关知识的微信
学软件开发去哪
河源无限软件开发零售价
蓝思网络技术有限公司一
exl根据选项读取数据库
网络安全教育个人信息有哪些
互联网络技术有限公司
数据库终期答辩的问题
固态盘装数据库安全吗
有线电视服务器请求失败怎么办
网络安全教育平台杭州登陆
香港软件开发公司排行
杭州区块链钱包软件开发
瞻博网络安全性
云南熬鹰网络技术有限公司
特定网络技术转让价目表
华为服务器客户集成商
网络安全美术作业
山东软件开发城市
网络安全自查中期报告
软件开发职场晋升路线
天门定制软件开发方案
赛亚网络安全布局
网络安全领域信息化
慈溪慈纵软件开发经营部
张掖网络安全技能大赛
生活中网络安全遇到的问题有哪些