背景:小程序一站式部署:從騰訊云服務(wù)器買(mǎi)了一套, 配置好業(yè)務(wù)服務(wù)器 php nginx;
- 登錄服務(wù)器, 在usr - share - nginx - html下 添加php文件: connect.php
<?php
echo "hello";
?>
- 小程序配置
在項(xiàng)目配置信息中添加request、socket、uploadFile合法域名等~
3.小程序代碼
首先: connect.wxml
<button bindtap="btn_click_fc">
connect
</button>
然后: connect.js
Page({
data:{
},
onLoad:function(options){
// 生命周期函數(shù)--監(jiān)聽(tīng)頁(yè)面加載
},
btn_click_fc:function(){
wx.request({
url: 'https://request合法域名/connect.php', //request合法域名就是小程序配置信息中的那個(gè)域名地址,系統(tǒng)分配的
method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
// header: {}, // 設(shè)置請(qǐng)求的 header
header: {
'content-type': 'application/json'
},
success: function(res){
// success
console.log(res.data);
},
fail: function(res) {
// fail
},
complete: function(res) {
// complete
}
})
}
})
4.結(jié)果 說(shuō)明小程序與服務(wù)器連接沒(méi)有問(wèn)題
在小程序調(diào)試界面,輸出success
- php服務(wù)器連接數(shù)據(jù)庫(kù): connect.php 更新代碼
<?php
$ip="數(shù)據(jù)庫(kù)ip地址"; //一站式部署中,為數(shù)據(jù)庫(kù)內(nèi)網(wǎng)地址
$dbuser="root"; //數(shù)據(jù)庫(kù)初始用戶名和密碼
$psw="password";
$dbname="test";
$conn = new mysqli($ip, $dbuser, $psw, $dbname );
if($conn->connect_error){
die("connection failed:" .$conn->connect_error);
}
$sql="SELECT *FROM test";
$result= $conn->query($sql);
if($result->num_rows >0){
while($row=$result->fetch_assoc()){
echo "id:" .$row["id"]. ",name:".$row["name"];
}
}else{
echo "query failed";
}
?>
6.微信小程序代碼不變,結(jié)果輸出數(shù)據(jù)庫(kù)中的數(shù)據(jù)
7.成功!!!