Android PHP MySQL : Images and Text – Select then Fill ListView Part 4 [PHP Script]

Android PHP MySQL : Images and Text – Select then Fill ListView Part 4 [PHP Script]







Part 3 : PHP Script

Below is our PHP script we used in this tutorial.You need to use your own database credentials.

  • The script selects data from mysql database.
  • It then encodes it into JSON format.
  • When we make a GET request we end up with the JSON array.

 

Quick Menu :

  1. Introduction
  2. Classes - Java Code.
  3. Layouts - XML Code.
  4. Server Side - PHP Code.

 

 

<?php
$host='127.0.0.1';
$username='root';
$pwd='';
$db="spacecraftDB";
$con=mysqli_connect($host,$username,$pwd,$db) or die('Unable to connect');
if(mysqli_connect_error($con))
{
  echo "Failed to Connect to Database ".mysqli_connect_error();
}
$sql="SELECT * FROM spacecraftTB";
$result=mysqli_query($con,$sql);
if($result)
{
  while($row=mysqli_fetch_array($result))
  {
    $data[]=$row;
  }
  
  print(json_encode($data));
}
mysqli_close($con);
?>

 

Test your script on the browser after creating database and adding data.If right you should see JSON like below :

JSON

 



    Leave a Reply

    Your email address will not be published. Required fields are marked *

    14 − four =

    COMMENTS