1. What is WAMP Server
WAMP is acronym for Windows, Apache, MySQL and PHP, Perl, Python. WAMP software is one click installer which creates an environment for developing PHP, MySQL web application. By installing this software you will be installing Apache, MySQL and PHP. Alternatively you can use XAMP Server also.
2. Installing and Running WAMP Server
Download & Install WAMP server from www.wampserver.com/en/. Once you have installed wamp server, launch the program from Start -> All Programs -> WampServer -> StartWampServer.
Following is a screen cast of Downloading and Installing WAMP Server.
3. Creating and Running PHP Project
Now you have the environment ready to develop a PHP & MySQL project. Go to the location where you installed WAMP server (In my case i installed in C:\wamp\) and go to www folder and create a new folder for your project. You have to place all your project files inside this folder.
Create a folder called android_connect and create a new php file called test.php and try out simple php code. After placing following code try to openhttp://localhost/android_connect/test.php and you should see a message called “Welcome, I am connecting Android to PHP, MySQL“.
Following is a screen cast of Creating and Running a simple PHP project.
4. Creating MySQL Database and Tables
In this tutorial i am creating a simple database with one table. Through out this tutorial i am using same table to perform example operations. Now open phpmyadmin by opening the address http://localhost/phpmyadmin/ in your browser. You can use the PhpMyAdmin tool to create a database and a table.
I am creating a database named androidhive and a table called products.
Following is a screen cast of Creating database and tables in phpmyadmin
5. Connecting to MySQL database using PHP
Now the actual server side coding starts. Create a PHP class to connect to MySQL database. The main purpose of this class is to open a connection to database and close the connection whenever its not needed. So create two files called db_config.php and db_connect.php
db_config.php – will have database connection variables
db_connect.php – a class file to connect to database
Following is code for two php files
Usage: When ever you want to connect to MySQL database and do some operations use the db_connect.php class like this
6. Basic MySQL CRUD Operations using PHP
6.a) Creating a row in MySQL (Creating a new product row)
In your PHP project create a new php file called create_product.php and place the following code. This file is mainly for creating a new product in products table.
In the following code i am reading product data via POST and storing them in products table. At the end i am echoing appropriate JSON as response.
For the above code JSON response will be like
When POST param(s) is missing
When product is successfully created
When error occurred while inserting data
6.b) Reading a Row from MySQL (Reading product details)
Create a new php file called get_product_details.php and write the following code. This file will get single product details by taking product id (pid) as post parameter.
The json response for the above file will be
When successfully getting product details
When no product found with matched pid
6.c) Reading All Rows from MySQL (Reading all products)
We need a json to list all the products on android device. So create a new php file namedget_all_products.php and write following code.
And the JSON response for above code
Listing all Products