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
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.
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.
I'm trying to learn PHP code and I'm working on a tutorial I've found on androidhive (at this link).
I'm getting stuck when I request products data with a filter.
This is what androidhive does in code:
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("pid", pid));
and in PHP:
if (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["username"] = $result["username"];
...
// 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 1";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found 2";
// 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);
}
While I'm basically doing this:
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "a"));
And in PHP:
if (isset($_GET["username"])) {
$username = $_GET['username'];
// get a product from products table
$result = mysql_query("SELECT *FROM products WHERE username = $username");
So I'm only filtering products by a different value.
This however return me "No product found 2" so my result is empty i guess.
When I run the original code it works perfectly.
Anyone could help me? Thanks in advance!
As the username type is varchar you have to modify you query in the following way
if (isset($_GET["username"])) {
$username = $_GET['username'];
$result = mysql_query("SELECT *FROM products WHERE username = '$username'");
/// other code
}
If username in your database is a string, you need quotes around $username.
$result = mysql_query("SELECT *FROM products WHERE username = '$username'");
$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());