osoda osoda - 7 days ago 5
MySQL Question

How to populate an android spinner with data coming from mysql

I already have populated my android spinner with data coming from mysql. But instead of getting 1 data per type i get everything. Example i have 2 basketball in my database, the android spinner shows 2 basketball instead of 1

PHP

$sql = "select s_name from sport order by s_name asc;";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);

$result = mysqli_query($con,$sql);



while($row=mysqli_fetch_array($result))
{
$tmp[] = $row;
}


echo json_encode ($tmp);

mysqli_close($con);
?>


Here is the response i get. Instead of getting only 1 value. I get everything from s_name in my android spinner

[{"0":"Basketball","s_name":"Basketball"}, {"0":"Basketball","s_name":"Basketball"},
{"0":"Basketball","s_name":"Basketball"},

Answer

You can use a group by in your query :

$sql = "select s_name from sport GROUP BY s_name order by s_name asc;";
Comments