I have this code passing a variable from a url. When i use $_GET method it returns me a json with no products found but when i give manually the the value that $user_email has from the url it returns me the correct json! what is wrong and how can i correct it? thank you
URL: http://***********/android_connect/get_all_products.php?user_email=m
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
$user_email= $_GET['user_email'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$test= "SELECT *FROM products WHERE user_email= '" .$user_email. "'";
//echo $test;
$result = mysql_query($test) 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["pid"] = $row["pid"];
$product["firstname"] = $row["firstname"];
$product["lastname"] = $row["lastname"];
$product["email"] = $row["email"];
$product["phone"] = $row["phone"];
$product["address"] = $row["address"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
$product["user_email"] = $row["user_email"];
// 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);
}
?>
Try this:
$test= "SELECT * FROM products WHERE user_email = '$user_email'";
EDIT:
$test= "SELECT * FROM products WHERE user_email = $user_email";
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 getting null results when I use the following Like query in PHP Script
$MasjidName = $_GET['MasjidName'];
$Percent = "%";
$search = $Percent.$MasjidName.$Percent;
echo $search;
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '".$search."'";
// get a product from products table
$result = mysql_query($sql) or die(mysql_error());
I have tried the following too
$result = mysql_query("SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%moh%'") or die(mysql_error());
The following is the null result I have been getting
{"masjids":[{"MasjidName":null,"Address":null,"Latitude":null,"Longitude":null}],"success":1,"masjid":[]}
whole code added below the following is the script i have been trying to get work
<?php
$response = array();
require_once dirname(__FILE__ ). '/db_connect.php';;
$db = new DB_CONNECT();
if (isset($_GET["MasjidName"]))
{
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) 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);
}
// 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);
}
?>
Try this:
$MasjidName = $_GET['MasjidName'];
$MasjidName = mysql_real_escape_string($MasjidName); // you have to escape your variable here.
$sql = "SELECT * FROM `MasjidMaster` WHERE `MasjidName` LIKE '%$MasjidName%'";
$result = mysql_query($sql) or die(mysql_error());
Try using
$sql = "SELECT * FROM MasjidMaster WHERE MasjidName LIKE '".$search."'";
I tried SELECT * FROM Customers WHERE City LIKE 's%'; and it gave me perfectly fine result. But when i tried SELECT * FROM 'Customers' WHERE 'City' LIKE 's%'; it gave me a null result.
Just remove '' and give it a try. Hope it helps.
$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());
Does anyone know of a way that I can gat data from 2 tables in a MySql DB using PHP? My android app allows users to place items in a shopping cart(table in MySql). When the user clicks on the shopping cart button on in the app it loads the "Shopping Cart Activity" and loads a listview with everything they have added to their cart based on a "Session ID" or "Customer ID" if they are logged in. What I do right now is when the item is added to the cart I capture the Product ID, Product Name, and Image URL, Quantity, and Price. It works great and quickly. What I am wanting to do is since the Product Name, image url, and price are already in the "Products Table" I want to do the same thing but when the user adds an item to their cart only capture the Product ID and session ID. When the customer goes to the Shopping Cart Activity I want my PHP Script to grab the Product ID in the Shopping_Cart table then Search the Products Tables for that Product ID and return the Price, Image URL, Name, etc.
Here is my PHP Script:
if($The_Function=="LOAD_CART"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();
$myItems["product_id"] = $row["PID"];
$myItems["product_name"] = $row["Item"];
$myItems["product_price"] = $row["Price"];
$myItems["product_qty"] = $row["Qty"];
$myItems["image_url"] = $row["URL"];
$myItems["line_id"] = $row["ItemCount"];
$line_total = $row["Price"] * $row["Qty"];
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// 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);
}
}
Here is what I am trying to do. This code does not work.
if($The_Function=="LOAD_CART2"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT * FROM shopping_cart WHERE SessionID LIKE '$user_session' AND Status LIKE '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$line_total = $row["Price"] * $row["Qty"];
$line_ID = $row["ItemCount"];
$Quantity = $row["Qty"];
$Prod_ID = $row["PID"];
$result2 = mysql_query("SELECT * FROM products WHERE pid LIKE '$Prod_ID'");
$myItems = array();
$theItem=$row["name"];
$thePrice=$row["price"];
$theImage=$row["prod_image"];
$myItems["line_id"] = $line_ID;
$myItems["product_qty"] = $Quantity;
$myItems["product_id"] = $Prod_ID;
$myItems["product_name"] = $theItem;
$myItems["product_price"] = $thePrice;
$myItems["image_url"] = $theImage;
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// 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);
}
THIS IS WHAT I JUST TRIED:
if($The_Function=="LOAD_CART2"){
$response = array();
require_once __DIR__ . '/db_connect.php';
$con = new DB_CONNECT();
$user_session = $_GET['session'];
$user_item_status = $_GET['status'];
$order_total = 0.00;
$result = mysql_query("SELECT s.*, p.* FROM shopping_cart AS s LEFT JOIN products AS p ON p.pid = s.PID WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'");
if(!empty($result)){
if (mysql_num_rows($result) > 0) {
$response["cart_contents"] = array();
//$result = mysql_fetch_array($result);
while ($row = mysql_fetch_array($result)) {
$myItems = array();
//FROM shopping_cart Table
$line_total = $row["Price"] * $row["Qty"];
$myItems["line_id"] = $row["ItemCount"];
$myItems["product_qty"] = $row["Qty"];
$myItems["product_id"] = $row["PID"];
//FROM product TABLE
$myItems["product_name"]=$row["p.name"];
$myItems["product_price"]=$row["p.price"];
$myItems["image_url"]=$row["p.prod_image"];
}
$order_total = $order_total + $line_total;
array_push($response["cart_contents"], $myItems);
}
// success
$response["success"] = 1;
$response["order_total"] = $order_total;
// user node
// $response["products"] = array();
// 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);
}
My Scheme:
products TABLE
pid|name|price|created_at|prod_image|description|catagory
shopping_cart TABLE
FirstName|LastName|OrderNumber|CustomerID|Email|Price|Qty|Status|URL|PID|SessionID|CustomerType|ItemCount
You should use a LEFT JOIN instead of doing a separate query for each item.
Something like:
"SELECT s.*, p.* FROM shopping_cart AS s
LEFT JOIN products AS p ON p.pid = s.PID
WHERE s.SessionID ='$user_session' AND s.Status = '$user_item_status'
Assuming lowercase pid is in the products table, and capital PID is in the shopping_card table.