Reputation: 864
Good day I am using Response Native. I want to pull data from MYSQL database with Response Native without using PHP. I cannot retrieve data from the MYSQL port. I want to search the MYSQL database. In the second code that I pay, I am processing the server. I want a select sql command in the MySQL database. I want to run the select command in MYSQL based on the itemsUSERNAME data.
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()
}
}
Upvotes: 3
Views: 4513
Reputation: 1417
I am also new to react native. But here's how I achieved your goal.
I created a helping method in react native to fetch data from server.
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);
});
}
Then I called it in componentWillMount()
.
componentWillMount(){
this.responseList();
}
Then I created the backend code like this.
<?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;
Hope this helps.
Upvotes: 1