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
In this tutorial i am covering basic CRUD (Create, Read, Update, Delete) operations on MySQL database using PHP.
If you are a novice about PHP and MySQL i suggest, you to learn basic PHP and SQL here.
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
When products not found
6.d) 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.