Welcome to Connect Android with PHP & Mysql Series Part 1

Summary

In this tutorial i will show you how to create database, get data from PHP scripts & show it in json form which you access by a URL.

1.) Installing Wamp Server on Windows.

Go to http://www.wampserver.com/en/ download the Wamp as per your software architecture 64 bit or 32 bit

Wamp Folder

Wamp Folder

Then go to install location mine is C:\wamp (by default this is the install location for are users) open wampmanager.exe

Now you can test your wamp server by going to this address in browser http://localhost/ or http://127.0.0.1. If it’s opening default page of wamp then you’re good to proceed further.

Check the phpmyadmin also with the url : http://localhost/phpmyadmin (PHPMyAdmin helps us to create,manage,insert into the database,tables with Graphical User interface instead of MySQL Console window)

2.) Creating our first PHP Script to test

Open notepad or notepad++ (i am using Notepad++)

write this code

<?php
echo "Hello,World";
?>

<?php means opening tag for any PHP Script it’s like public class name { in java

and ?> means closing tag like closing bracket in JAVA

what ever you code write in these tag will be handle as PHP code

echo means System.out.println

Save it as test.php in c:\wamp\www (all files in this folder will be accessible from localhost)

now when you access it with this URL http://localhost/test.php it will print this Hello,World

Hello-World

Hello-World

3. )Let’s move to create database through PHPMYADMIN

open this http://localhost/phpmyadmin

Click on Database button on top header now it will take you to create database page

Localhost PHPMyAdmin

Localhost PHPMyAdmin

Enter DB (Database) name in first text box and create on create.

Create Database in PHPMyAdmin

Create Database

Now on left sidebar you will see list of database.Click on your newly created database then click on Create Table.

Create Table in PHPMyAdmin

Create Table

Fill fields

  1. Table Name : products
  2. No. of columns : 4

Columns

Products Table

Products Table

Name Type Length/Values A I (Auto Increment)
id INT Empty (By Default) Yes
title VARCHAR 40 No (By Default)
description VARCHAR 250 No (By Default)
img_url VARCHAR 100 No (By Default)

Now we will add a entry in MySQL Table named products

Select products from side panel.Click on Insert on Header

Now fill it like this

Column Type Function Value
id INT(11)
title VARCHAR(40) Rickshaw Racer
description VARCHAR(250) Get Rickshaw Racer on Google Play
img_url VARCHAR(100) http://s22.postimg.org/kg4dj2usx/Icon_Rickshaw_512.png

Now you have filled it with details click on Go Button.

Insert 1 or  more rows with the details you want to be shown in Android App.

4.) Move to PHP again to show our data by echo command

Let’s make a config.php file in the www folder in wamp directory.

Detail about config.php : It’s usefull to save all the MySQL database info in one file so you don’t have to fill it again & again.

  • Host = localhost (by default if you’re using it on wamp and MySQL Server is also installed on same machine)
  • Username = root (by default)
  • Password =  (by default Empty 0 length)
  • Database name = android_db (same)
  • Table name = products (same)
<?php
$host="localhost";
$user_name="root";
$password="";
$db_name="android_db";
$table_name="products";
//Let's make a mysql connection to connect the server.
$con=mysqli_connect($host,$user_name,$password);
mysqli_select_db($con,$db_name);
?>

Explanation by lines

1.) Opening Tag.

2.) Host name or Ip Address where our MySQL Server is installed.

3.) Username to access the database.

4.) Password for the username to get pass authentication.

5.) Database name in our case android_db which we made in earlier steps.

6.) Table name in our case products

8.) Comment just like we do in JAVA

9.) It’s connecting to MySQL Database to make communication

10.) Select database to communicate

5.) Create fetch.php to get results from table

Code

<?php
include_once("config.php");
$query=mysqli_query($con,"SELECT * FROM ".$table_name);
$array;
while($result=mysqli_fetch_assoc($query)){
$array[]=$result;
}
echo json_encode($array);
?>

Explanation by lines

2.) Include config.php just like Import in Java.

4.) Here we query the MySQL Database to give us all rows from table name

5.) Declaring array as a variable

6.) While Loop to fetch all results

8.)The $result fill all results in $array

11.) Print encoded JSON

try to access the file via Browser from this url http://localhost/fetch.php it will print something like this

[{"id":"1","title":"Rickshaw Racer","description":"Get Rickshaw Racer on Google Play","img_url":"http:\/\/s22.postimg.org\/kg4dj2usx\/Icon_Rickshaw_512.png"}]

So the PHP Part is completed in next Tutorial we will work on Android Code