0

In my react native application, I want show data that I get from my sql server using php.

The problem is my php file works fine( when I try it in postman or any browser) but when I run the application in my phone I got nothing, what can be the cause??

import React, { useEffect, useState } from 'react';
import { ActivityIndicator, FlatList, Text, View } from 'react-native';

export default App = () => {
  const [isLoading, setLoading] = useState(true);
  const [data, setData] = useState([]);

  useEffect(() => {
    fetch('http://192.168.1.10/ment.php')
      .then((response) => response.json())
      .then((json) => setData(json.movies))
      .catch((error) => console.error(error))
      .finally(() => setLoading(false));
  }, []);

  return (
    <View style={{ flex: 1, padding: 24 }}>
      {isLoading ? <ActivityIndicator/> : (
        <FlatList
          data={data}
          keyExtractor={({ id }, index) => id}
          renderItem={({ item }) => (
            <Text>{item.MOIS}</Text>
          )}
        />
      )}
    </View>
  );
};

And this is my php file that shows 5 months from my DB

<?php
include 'serverName.php';
$connectionInfo=array("Database"=>"Net","UID"=>"s","PWD"=>"123");
$conn=sqlsrv_connect($serverName,$connectionInfo);

$sql = "select TOP 5 MOIS from Dyn_Encaissement " ;  

$stmt = sqlsrv_query( $conn, $sql  );

 while( $row[] = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
      $Item = $row;
      $json = json_encode($Item);   
}
    echo $json;
?>
Asmaa
  • 1
  • 1
  • Is that IP correct for calling it from your phone? That is, does the PHP code actually execute? – droopsnoot Aug 28 '20 at 09:09
  • when I try the ip in my browser(computer) it works fine, do i have to check another thing? – Asmaa Aug 28 '20 at 09:13
  • Yes, check it from your phone as mentioned, not from your computer. If the phone is the problem then try it from there. If the phone isn't connected to the same wifi network as your computer then you'll have an immediate problem. If it's not the network, then you need to debug the react code and check for console errors etc. – ADyson Aug 28 '20 at 09:16
  • Sorry but how and what to check in my phone i didn't understand – Asmaa Aug 28 '20 at 09:18
  • 1
    Open this `http://192.168.1.10/ment.php` url in your phone's browser to see if this url is accessible from your phone. – Umair Khan Aug 28 '20 at 09:29
  • And if that works, then move onto to debugging the react application (see https://stackoverflow.com/questions/43415884/how-do-you-debug-react-native-when-it-is-running-on-device for example as a guide to that process, or you can google for others). – ADyson Aug 28 '20 at 09:59
  • 1
    192.168.1.10 is a non-routable address, so you won't be able to open it directly over the internet, only if your phone is on the same LAN as the computer. If you are trying to get to it over the internet via your data connection, you will need to have the PHP code hosted somewhere that can be accessed externally. – droopsnoot Aug 28 '20 at 10:10
  • To clarify more, this project used to work perfectley (I am using local php file and to get data I'm connecting on the same wifi ) but I don't know why when I run the app this day doesn't work like it's not getting the adress(btw if I do another ip like 192.168.1.3 they are giving me network failed and when I do the correct ip i got nothing but after like min they show network failed) what can be the reason?? – Asmaa Aug 28 '20 at 10:35
  • are you sure your computer's IP address hasn't changed? If your local router is using DHCP it can issue new IP addresses to each device from time to time, unless you've specifically configured it to use static addresses. – ADyson Aug 28 '20 at 13:09
  • I'm sure that it's correct ip (I got it using ipConfig ) – Asmaa Aug 28 '20 at 13:21

0 Answers0