I am learning to create an API to return JSON for my Android App.
http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
I am not getting the JSON response if i add this line
$product["Area"] = $row["Area"];
If i remove the Above line from the below file. Then i am getting the response.
I have even checked the DB. I can't figure it why it happens.
Thanks in Advance
This is my PHP File (get_all_products)
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT * FROM shelter") or die(mysql_error());
// check for empty result
if (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["ID"] = $row["ID"];
$product["Timestamp"] = $row["Timestamp"];
$product["Accomodation"] = $row["Accomodation"];
$product["Area"] = $row["Area"];
// 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);
}
?>
SELECT *FROM Shelter
it looks a bit odd, shouldn`t it be
SELECT * FROM Shelter
also see if you have a column Area in shelter table and has valid data in it.
Related
I'm trying to convert an Array to JSON in PHP I have two identical pieces of code except for them referencing to different databases and data. could someone help me?
The code that works:
get_all_products.php
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_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 result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp product array
$product = array();
$product["pid"] = $row["pid"];
$product["pname"] = $row["pname"];
$product["pprice"] = $row["pprice"];
$product["pamount"] = $row["pamount"];
// 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);
}
?>
The one that doesn't work:
post2.php
<?php
/*
* Following code will list all the users
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all users from users table
$result = mysql_query("SELECT * FROM users") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// users node
$response["users"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$user = array();
$user["ID"] = $row["ID"];
$user["name"] = $row["name"];
$user["balance"] = $row["balance"];
$user["pin"] = $row["pin"];
// push single user into final response array
array_push($response["users"], $user);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no users found
$response["success"] = 0;
$response["message"] = "No users found";
// echo no users JSON
echo json_encode($response);
}
?>
I Asked my teacher too but he didn't know the answer either.
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE ('%moh%')") or die mysql_error();
The error i get is
Parse error: syntax error, unexpected T_STRING in /home/maximtec/public_html/masjid_folder/MasjidFinderScripts/find_by_name.php on line 24
This query does work when i use it in MySQL but it doesn't when I place it in a PHP Script
Please suggest a solution
------------EDIT :After changing query from the received answers-------------------------------------
Well I updated my query but now I am getting null results
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
Following is my full script :
<?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 class
//require_once __DIR__ . '/db_connect.php';
require_once dirname(__FILE__ ). '/db_connect.php';;
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["MasjidName"])) {
$MasjidName = $_GET['MasjidName'];
// get a product from products table
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
$response["masjids"] = array();
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$row = mysql_fetch_array($result);
$masjid = array();
$masjid["MasjidName"] = $row["MasjidName"];
$masjid["Address"] = $row["Address"];
$masjid["Latitude"] = $row["Latitude"];
$masjid["Longitude"] = $row["Longitude"];
// success
$response["success"] = 1;
// user node
$response["masjid"] = array();
array_push($response["masjids"], $masjid);
// array_push($response["masjid"], $masjid);
}
// 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);
}
?>
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
A little more tweaking, for good practice wrap table names and table columns in ``.
You also shouldn't need () around ('%moh%')
Try this-
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE ('%moh%')") or die(mysql_error());
You forget parentheses with die(mysql_error())
try this:
$result = mysql_query("SELECT * FROM MasjidMaster WHERE MasjidName LIKE '%moh%' ") or die(mysql_error());
use die() like this die(mysql_error()) .
I have a problem with my PHP query. I just need to get the prescription for the clicked patient in listview.
Here is the table structure for prescription.
I need patient with id 79 to display all his corresponding prescription. Please help me.
Here is the PHP.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
if (isset($_GET["uid"])) {
$uid = $_GET['uid'];
// get all products from products table
$result = mysql_query("select * from prescription") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["prescription"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$prescription = array();
$prescription["pres_id"] = $row["pres_id"];
$prescription["pres_name"] = $row["pres_name"];
//$product["price"] = $row["price"];
// push single product into final response array
array_push($response["prescription"], $prescription);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No prescriptions found";
// echo no users JSON
echo json_encode($response);
}
}else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
}
?>
To get all the prescriptions for a selected patient, wouldn't it be:
mysql_query("select * from prescription where patient_user_fkey = '$patientID'")or die(mysql_error());
instead of:
mysql_query("select * from prescription") or die(mysql_error());
I create php file to get all task from database but when I enter the url "localhost/evisiting/get_all_task.php" in browser it get all task details but with null data in each row. However I populate my database table. Kindly guide how can i get these values from database not the null values..
Its my php file:
get_all_task.php
<?php
/*
* Following code will list all the tasks
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all task from my_task table
$result = mysql_query("SELECT *FROM my_task") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// task node
$response["my_task"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$my_task = array();
$my_task["cid"] = $result["cid"];
$my_task["cus_name"] = $result["cus_name"];
$my_task["contact_number"] = $result["contact_number"];
$my_task["ticket_no"] = $result["ticket_no"];
$my_task["task_detail"] = $result["task_detail"];
// push single task into final response array
array_push($response["my_task"], $my_task);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no task found
$response["success"] = 0;
$response["message"] = "No task found";
// echo no users JSON
echo json_encode($response);
}
?>
You're using the wrong variable inside your while() loop. $result is your query result handle, NOT the row you fetched:
$my_task["cid"] = $result["cid"];
^^^^^^^--- should be $row
As well, you're spitting out a lot of code to fetch individual fields when you could simply have this:
$result = mysql_query("SELECT cid, cus_name, contact_number, .... FROM my_task") or die(mysql_error());
$response['my_task'] = array();
while($row = mysql_fetch_assoc($result)) {
$response['my_task'][] = $row;
}
exact same end-result, but far less repetition of all those field names - if you want to add a new field into this result, you simply at it to the SELECT statement, and the rest of the code handles it automatically. If you need to change the field name in the $response array, simply alias it in the query.
i'm having a problem where for instance: we have 2 users, so user A insert the data into the database and it can be retrieved with no problem. User B insert the data into the database and when user B want to retrieve the data, the user B gets the data of user A not user B.
What will be causing that problem?
Here my php code for getting data from mysql database:
<?php
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["subject_id"])) {
$subject_id = $_GET['subject_id'];
// get a product from products table
$result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["subject_id"] = $result["subject_id"];
$product["lecturer_name"] = $result["lecturer_name"];
$product["time_offered"] = $result["time_offered"];
$product["subject_details"] = $result["subject_details"];
//$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 subject found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No subject 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);
}
?>
// get a product from products table
$result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = $subject_id");
should be
// get a product from products table
$result = mysql_query("SELECT * FROM subject_offered WHERE subject_id = '$subject_id' ");
//some times when you miss '' this single commas then it also create a problem