美好的一天,我正在使用 Response Native。我想在不使用 PHP 的情况下使用 Response Native 从 MYSQL 数据库中提取数据。我无法从 MYSQL 端口检索数据。我想搜索MYSQL数据库。在我支付的第二个代码中,我正在处理服务器。我想在 MySQL 数据库中使用 select sql 命令。我想根据 itemsUSERNAME 数据在 MYSQL 中运行 select 命令。
var mysql = require('mysql');
var con = mysql.createConnection({
...
});
export default class usrFirst extends React.Component {
constructor(props) {
super(props)
this.state = {
itemsUSERNAME:[],
}
connection.connect()
componentDidMount() {
connection.query('SELECT ID from solution where itemsUSERNAME', function (err, rows, fields) {
if (err) throw err
})
connection.end()
}
}
最佳答案
我也是 React Native 的新手。但我是这样实现你的目标的。
我在 React Native 中创建了一个帮助方法来从服务器获取数据。
responseList() {
fetch('YOUR API', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: itemsUSERNAME
})
}).then((response) => response.json())
.then((responseJson) => {
this.setState({ package: responseJson.results });
}).catch((error) => {
console.error(error);
});
}
然后我在 componentWillMount()
中调用了它。
componentWillMount(){
this.responseList();
}
然后我创建了这样的后端代码。
<?php include 'db.php'; ?>
<?php
$arr = array();
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
$data = $obj['data'];
$sql = "SELECT ID from solution where itemsUSERNAME = '" . $data . "'"; // This is just my code example.
$result_set = mysqli_query($conn, $sql);
while ($result = mysqli_fetch_array($result_set)) {
array_push($arr, $result);
}
$responseJson = json_encode(array('results' => $arr));
echo $responseJson;
希望这对您有所帮助。
关于mysql - React Native 获取 MYSQL 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58910181/