I'm working on creating an app and I'm pretty new to doing this. I'm using Android studio. I've created a php script and hosted it on my hostinger account, along with the database it's accessing. If I enter the URL for the script on my browser it gives me a warning saying that it needs a parameter (not sure if there is a way for me to enter a parameter when I test it like this?) and shows the array which I want returned (with all null values of course, since it doesn't have the parameter for the SELECT statement). When I try to call the php script from my code with the parameter it returns an "Error 404 - Page Not Found" error. This is where I store it to the string
RequestHandler rh = new RequestHandler();
String s = rh.sendGetRequestParam(Config.URL_GET_USER, username);
the URL it's referencing is definitely correct, as I can copy it straight from the code into the browser and it works. And I've printed the username string out on the line before to make sure it's correct and it is. Here's the relevant sections of code from the RequestHandler class
public String sendGetRequestParam(String requestURL, String username){
StringBuilder sb =new StringBuilder();
try {
URL url = new URL(requestURL+username);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String s;
while((s=bufferedReader.readLine())!=null){
sb.append(s+"
");
}
}catch(Exception e){
}
return sb.toString();
}
And finally here is the PHP script
<?php
$username = $_GET['username'];
require_once('dbConnect.php');
$sql = "SELECT * FROM user_info WHERE username=$username";
$r = mysqli_query($con,$sql);
$result = array();
$row = mysqli_fetch_array($r);
array_push($result,array(
"user_id"=>$row['id'],
"username"=>$row['username'],
"password"=>$row['password'],
"dob"=>$row['dob'],
"forename"=>$row['forename'],
"surname"=>$row['surname']
));
echo json_encode(array('result'=>$result));
mysqli_close($con);
Let me know if you need any more information
EDIT: I've now added ?username= to the request so it's retrieving the array but the array is filled with null values. I think the error message I get when I try to access it through the browser is related to why this happens. It says: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in" then it has the path to the php file and says line 12, so that's this line:
$row = mysqli_fetch_array($r);