6.b) Membaca Baris dari MySQL (Membaca detail produk)
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.
<?php/* * Following code will get single product details * A product is identified by product id (pid) */// array for JSON response$response = array();// include db connect classrequire_once __DIR__ . '/db_connect.php';// connecting to db$db = new DB_CONNECT();// check for post dataif (isset($_GET["pid"])) { $pid = $_GET['pid']; // get a product from products table $result = mysql_query("SELECT *FROM products WHERE pid = $pid"); if (!empty($result)) { // check for empty result if (mysql_num_rows($result) > 0) { $result = mysql_fetch_array($result); $product = array(); $product["pid"] = $result["pid"]; $product["name"] = $result["name"]; $product["price"] = $result["price"]; $product["description"] = $result["description"]; $product["created_at"] = $result["created_at"]; $product["updated_at"] = $result["updated_at"]; // success $response["success"] = 1; // user node $response["product"] = array(); array_push($response["product"], $product); // echoing JSON response echo json_encode($response); } else { // no product found $response["success"] = 0; $response["message"] = "No product found"; // echo no users JSON echo json_encode($response); } } else { // no product found $response["success"] = 0; $response["message"] = "No product found"; // echo no users JSON echo json_encode($response); }} else { // required field is missing $response["success"] = 0; $response["message"] = "Required field(s) is missing"; // echoing JSON response echo json_encode($response);}?> |
The json response for the above file will be
When successfully getting product details
{ "success": 1, "product": [ { "pid": "1", "name": "iPHone 4S", "price": "300.00", "description": "iPhone 4S white", "created_at": "2012-04-29 01:41:42", "updated_at": "0000-00-00 00:00:00" } ]} |
When no product found with matched pid
{ "success": 0, "message": "No product found"} |
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 named get_all_products.php and write following code.
<?php/* * Following code will list all the products */// array for JSON response$response = array();// include db connect classrequire_once __DIR__ . '/db_connect.php';// connecting to db$db = new DB_CONNECT();// get all products from products table$result = mysql_query("SELECT *FROM products") or die(mysql_error());// check for empty resultif (mysql_num_rows($result) > 0) { // looping through all results // products node $response["products"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $product = array(); $product["pid"] = $row["pid"]; $product["name"] = $row["name"]; $product["price"] = $row["price"]; $product["created_at"] = $row["created_at"]; $product["updated_at"] = $row["updated_at"]; // push single product into final response array array_push($response["products"], $product); } // success $response["success"] = 1; // echoing JSON response echo json_encode($response);} else { // no products found $response["success"] = 0; $response["message"] = "No products found"; // echo no users JSON echo json_encode($response);}?> |
And the JSON response for above code
Listing all Products
{ "products": [ { "pid": "1", "name": "iPhone 4S", "price": "300.00", "created_at": "2012-04-29 02:04:02", "updated_at": "0000-00-00 00:00:00" }, { "pid": "2", "name": "Macbook Pro", "price": "600.00", "created_at": "2012-04-29 02:04:51", "updated_at": "0000-00-00 00:00:00" }, { "pid": "3", "name": "Macbook Air", "price": "800.00", "created_at": "2012-04-29 02:05:57", "updated_at": "0000-00-00 00:00:00" }, { "pid": "4", "name": "OS X Lion", "price": "100.00", "created_at": "2012-04-29 02:07:14", "updated_at": "0000-00-00 00:00:00" } ], "success": 1} |
When products not found
{ "success": 0, "message": "No products found"} |
