I have a database with football players from FIFA and I'm able to search it from my Android application based on their names. I want to be able to search it based on the players rating as well, but I'm not able to do that.
This is how I try to send the two criteria I have (player name and rating) from my application and this is where it doesn't work. It works if I just have one Uri.builder() with the searchQuery variable which is the name of the player. But when I try to add another Uri.builder() with the variable minRating it can't make the connection to the database.
// Setup HttpURLConnection class to send and receive data from PHP and MySQL
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput to true as we send and recieve data
conn.setDoInput(true);
conn.setDoOutput(true);
// add parameter to our above url
Uri.Builder builder = new Uri.Builder().appendQueryParameter("searchQuery", searchQuery);
String query = builder.build().getEncodedQuery();
Uri.Builder builder2 = new Uri.Builder().appendQueryParameter("minRating", minRating);
String minRating = builder2.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.write(minRating);
writer.flush();
writer.close();
os.close();
conn.connect();
This is how I receive the data with my PHP-file and query the database:
<?php
include 'config.inc.php';
if(isset($_POST['searchQuery']))
{
$search_query=$_POST['searchQuery'];
$min_rating=$_POST['minRating'];
$sql = "SELECT Name, Club_Position, Rating from spillere where Name LIKE '%$search_query%'
AND Rating > $min_rating";
$statement = $conn->prepare($sql);
$statement->bindParam(':search_query',$search_query, PDO::PARAM_STR);
$statement->execute();
if($statement->rowCount())
{
$row_all = $statement->fetchall(PDO::FETCH_ASSOC);
header('Content-type: application/json');
echo json_encode($row_all);
}
elseif(!$statement->rowCount())
{
echo "no rows";
}
}
?>
This is what my stack trace looks like:
How can I take two or more search criteria from my Android application, send it to the PHP-file and query the database?
searchQueryandminRating. You can do the same to send further parameters. Or what exactly do you want?searchQuery) . When I try to send (minRating) it doesn't work. So there must be something wrong with the way that I use the (Uri.builder).