Android MySQL Fetch Values from Database Using PHP

Android MySQL :

Android MySQL, In this part of tutorial we will be dealing with the fetching values into MYSQL database with the help of php. php is the open source server side scripting language which is utilized for creating an api.

In the previous part of the tutorial we have dealt with the insert of values into the database.Now let’s go through how we can fetch the data from android mysql database.

As you may have already know the CRUD operations if not please refer my previous tutorial.

https://androidcoding.in/2019/01/26/insert-into-php/

 

So let’s get started now

Step 1:

Connect.php

This is first step of connecting to the database here we provide the details required

 

  1.  Hostname is the place where the database is.
  2.  Username for the user access.
  3.  Password  is required for maintaining security for local host this will be empty.
  4.  DBName  this will select the database out of all your databases present on your server

 

define('Hostname','localhost'); //db host
define('Username','root'); // db username
define('Password',''); //db password
define('DBName','userS'); // db name

 

Using all these parameters will help us connect android mysql db

 $con = mysqli_connect(Hostname,Username,Password,DBName) or die('Cannot Connect');

With the help of the connect file we can establish a connection to database and from there execute transactions by passing query’s like insert, delete, update etc.,

 

Step 2:

Fetch.php

The first step is to connect to database using the above mentioned process by specifying the file at start

require_once('connect.php');

 

Then accept the inputs from the user via API i.e., the key parameter by which we can search the table to find the exact record we need to fetch.

Save the value and save it to variable as below

$name = $_POST['name'];

 

Now write a SQL query to select the values from table using the key parameter as the constraint.

$sql = "select * from user_table where name = '$name'";

 

Now call the connection and thereby execute the SQL query as

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

 

Create an array to store the result accordingly

 $result = array();

 

fetch the data from tables according to the row wise as

'name'=>$row[1],
'age'=>$row[2],
'phone'=>$row[3],
'email'=>$row[4]

 

echo the encoded array list

 echo json_encode(array("result"=>$result));

 

finally close the database

mysqli_close($con);

 

For full detailed android mysql video tutorial visit the link below

If you have any query’s in this tutorial on android mysql do let us know in the comment section below.If you like this android mysql tutorial do like and share for more interesting updates.

 

 

 

Show Buttons
Hide Buttons
Read previous post:
Insert MYSQL || Insert data into MYSQL using PHP

  Android Insert MYSQL : This tutorial android insert MYSQL deals with the PHP integrating MYSQL database to insert a...

Close