Let`s talk about old technique, with new start.
MySQL with sever details, After Long time, on blogger. I find something new, let`s write about MYSQL database.
In this we will call a PHP script to perform basic CRUD(Create, Read, Update, Delete) operations. To brief you on the architecture, it works. your android app calls First a PHP script in order to perform a data operation, lets say “Create”. Then PHP script connects to MySQL database to perform the operation.
So it start data flows from your Android app to PHP script then finally is stored in MySQL database.
Let`s first we need to discuss over something new for android developer.
start the program from Start -> All Programs -> WampServer -> StartWampServer.
You can test your server by opening the address http://localhost/ in your browser.
Also you can check phpmyadmin by opening http://localhost/phpmyadmin
Create a folder called Makersofandroid and create a new php file called test.php and try out simple php code. After placing following code try to open http://localhost/android_connect/test.php and you should see a message called “Welcome, I am connecting Android to PHP, MySQL“.
makersofandroid.php
MySQL with sever details, After Long time, on blogger. I find something new, let`s write about MYSQL database.
In this we will call a PHP script to perform basic CRUD(Create, Read, Update, Delete) operations. To brief you on the architecture, it works. your android app calls First a PHP script in order to perform a data operation, lets say “Create”. Then PHP script connects to MySQL database to perform the operation.
So it start data flows from your Android app to PHP script then finally is stored in MySQL database.
Let`s first we need to discuss over something new for android developer.
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.Way to Installing and Running WAMP Server
Download & Install WAMP server from www.wampserver.com/en/. Once you have installed wamp server,start the program from Start -> All Programs -> WampServer -> StartWampServer.
You can test your server by opening the address http://localhost/ in your browser.
Also you can check phpmyadmin by opening http://localhost/phpmyadmin
Start 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 Makersofandroid and create a new php file called test.php and try out simple php code. After placing following code try to open http://localhost/android_connect/test.php and you should see a message called “Welcome, I am connecting Android to PHP, MySQL“.
makersofandroid.php
"<?php ?>" 4. Creating MySQL Database and TablesIn 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 makersofandroid and a table called products.
Now Connecting to MySQL database using PHP Script.Here, the code start for 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.phpdb_config.php – will have database connection variables db_connect.php – a class file to connect to database Following is code for two php files db_config.php
Here is Basic MySQL CRUD Operations using PHPIn this tutorial basic CRUD (Create, Read, Update, Delete) operations on MySQL database using PHP.Learn How to Creating a row in MySQL (Creating a new product)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 this code data read via POST and storing them in products table.
When POST param(s) is missing
Learn to 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.
When successfully getting product details
Learn to 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 named get_all_products.php and write following code.
Listing all Products
Learn to Updating a Row in MySQL (Updating product details)Create a php file named update_product.php to update product details. Each product is identified by pid.
Learn to Deleting a Row in MySQL (Deleting a product)The last operation is deletion on database. Create a new php file called delete_product.php and paste the following code. The main functionality of this file is to delete a product from database.
Here we come with android.Creating Android ApplicationCreate a new project in your Eclipse IDE by filling the required details.1. Create new project in Eclipse IDE by going to File ⇒ New ⇒ Android Project and name the Activity class name as MainScreenActivity. 2. Open your AndroidManifest.xml file and add following code. First adding all the classes & creating to manifest file. Also adding INTERNET Connect permission.
Displaying All Products in ListView (Read)5. Now we need an Activity display all the products in list view format. As we know list view needs two xml files, one for listview and other is for single list row. Create two xml files under res ⇒ layout folder and name it as all_products.xml and list_item.xmlall_products.xml
-> First a request is send to get_all_products.php file using a Background Async task thread. -> After getting JSON from get_all_products.php, i parsed it and displayed in a listview. -> If there are no products found AddNewProductAcivity is launched.
7. Create a new view and acivity to add a new product into mysql database. Create a simple form which contains EditText for product name, price and description. Create a new xml file and name it as add_product.xml and paste the following code to create a simple form. add_product.xml
-> First new product data is read from the EditText form and formatted into a basic params. -> A request is made to create_product.php to create a new product through HTTP post. -> After getting json response from create_product.php, If success bit is 1 then list view is refreshed with newly added product.
Reading, Updating and Deleting a Single Product9. If you notice the AllProductsActivity.java, In listview i am launching EditProductAcivity.java once a single list item is selected. So create xml file called edit_product.xml and create a form which is same as create_product.xml.edit_product.xml
-> First product id (pid) is read from the intent which is sent from listview. -> A request is made to get_product_details.php and after getting product details in json format, I parsed the json and displayed in EditText. -> After displaying product data in the form if user clicks on Save Changes Button, another HTTP request is made to update_product.php to store updated product data. -> If the user selected Delete Product Button, HTTP request is made to delete_product.php and product is deleted from mysql database, and listview is refreshed with new product list.
JSON Parser ClassI used a JSON Parser class to get JSON from URL. This class supports two http request methods GET and POST to get json from url.
Here is Suhan Gorya.
|