鍍金池/ 問(wèn)答/PHP  HTML/ 小程序如何將數(shù)據(jù)庫(kù)中的數(shù)據(jù)分類賦值到頁(yè)面里

小程序如何將數(shù)據(jù)庫(kù)中的數(shù)據(jù)分類賦值到頁(yè)面里

index.wxml
<view >
<view>{{x}}</view>
<view>{{y}}</view>
</view>

index.js
Page({
data:{
x:'',
y:''
},
onLoad: function (options) {
var that=this

wx.request({
  url: 'http://localhost/demo/demo.php',
 
  method: "GET",
  header: {
    'content-type': 'application/json'
  },
  success: function (res) {
    console.log(res.data);
    that.setData({
      不知道怎么寫
      我想把數(shù)據(jù)庫(kù)中的title給x text給y
    })
    },
  fail: function (res) {
     console.log('fail')
     }
})

},
})
php
<?php
//配置連接參數(shù)
$host = 'localhost';
$dbName = 'test';
$userName = 'root';
$password = 'root';
//連接到數(shù)據(jù)庫(kù)
$con = mysqli_connect($host,$userName,$password,$dbName);
if(mysqli_errno($con)){
echo mysqli_errno($con);
}
$sql = "select title,text from demo";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);
echo $row["title"].$row["text"];
?>

數(shù)據(jù)庫(kù)只能打印出來(lái)才能有數(shù)據(jù)嗎

回答
編輯回答
尤禮

官方說(shuō)明
開(kāi)發(fā)的時(shí)候可以跳過(guò)域名檢測(cè),還有一個(gè)就是不起效果的時(shí)候應(yīng)該會(huì)提示你什么的?

2017年5月2日 09:30
編輯回答
下墜

你的問(wèn)題其實(shí)很簡(jiǎn)單,php收到請(qǐng)求,轉(zhuǎn)到mysql去查詢所需要的數(shù)據(jù),php再將查詢到的數(shù)據(jù)處理成特定的數(shù)據(jù)格式并返回

php中返回json使用 echo json_encode($row);

你的小程序?qū)?yīng)的獲取數(shù)據(jù)應(yīng)該就是

success: function(res) {
    var title = res.title;
    var content = res.text;
    
    this.setData({
        x: title,
        y: content,
    });
}

-----------分割線----------

你應(yīng)該是沒(méi)有看到這個(gè) (|3[____]

PS:有條件還是把“開(kāi)發(fā)設(shè)置”里的服務(wù)器和域名設(shè)置了,上線也是需要的v( ̄? ̄)v

圖片描述

2017年9月15日 01:02