I have tried getting the value of player id from my table barrel_user using post request my only problem is two response is not working but if I give only one it works
$customer_query="SELECT player_id FROM barrel_user where phone_number='$phone_number'";
$fetchresult=mysqli_query($conn,$customer_query);
if(mysqli_num_rows($fetchresult)>0)
{
while($rows = mysqli_fetch_assoc($fetchresult)){
// $response=$rows;
if ($fetchresult) {
$response["success"] = 1;
//$response["player_id"]= $player_id;
$player_id= $rows['player_id'];
$response=$rows;
} else {
$response["success"] = 0;
}
// $response["success"] = 'User Exists';
}
}
above is my php code for a post request
I am not getting this value
$response["success"] = 1;
but I am getting the value of player_id
note:- the code works fine and if I remove $response=$rows I am getting the message success=1
Try this
$response=array();
$customer_query="SELECT player_id FROM barrel_user where phone_number='$phone_number'";
$fetchresult=mysqli_query($conn,$customer_query);
if(mysqli_num_rows($fetchresult)>0)
{
while($rows = mysqli_fetch_assoc($fetchresult)){
if ($fetchresult) {
$response["success"] = 1;
//$response["player_id"]= $player_id;
$player_id= $rows['player_id'];
$response[]=$player_id;
} else {
$response["success"] = 0;
}
}
}
Related
I'm trying to get my data from database and I can get it but there is an error I'm getting two copies of those records. I don't understand why my looping is not working. You can find the code below. I didn't initiate a php code so it would not be a SQL injection. Please Guide.
if( mysqli_num_rows( $result ) > 0 ) {
while($row = mysqli_fetch_array($result))
{
$flag[checkdate]=$row[checkdate];
$flag[checkno]=$row[checkno];
$flag[datepaid]=$row[datepaid];
$flag[clientname]=$row[clientname];
$flag[bank]=$row[bank];
$flag[amount]=$row[amount];
$flag[status]=$row[status];
array_push($info, $flag);
}
$response["success"] = true;
$response["message"] = $info;
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "No entries yet";
echo json_encode($response);
}
I have 2 database tables
main_menu which has 3 columns id, item_name, img
sub_menu which has columns id, sub_item_name, item_name, add_price, img
Now I am trying to get all sub menu under master menu when user clicks on master menu
eg.
select * from sub_menu where item_name="Cafe"
it should display all sub menu under Cafe.
And here is the web service I wrote to do that and get data in json format
<?php
$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["item_name"])) {
// get a product from products table
$result = mysql_query("SELECT * FROM sub_menu WHERE item_name ='".$_GET["item_name"]."'");
echo $result;
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$sub_menu = array();
$sub_menu["id"] = $result["id"];
$sub_menu["item_name"] = $result["item_name"];
$sub_menu["sub_item_name"] = $result["sub_item_name"];
$sub_menu["add_price"] = $result["add_price"];
$sub_menu["img"] = $result["img"];
// success
$response["success"] = 1;
// user node
$response["sub_menu"] = array();
array_push($response["sub_menu"], $sub_menu);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No sub menu found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No menu 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);
}
?>
Now the problem is I am only getting first data only, it should display all data
Query is working fine in mysql, I checked that. The problem is somewhere in my php service.
You have to call mysql_fetch_array() in a loop, and add all the rows to an array. You can just copy the whole row of results in a single assignment, since you were using the same field names in your JSON as in the database.
if (mysql_num_rows($result) > 0) {
$submenu = array();
while ($result = mysql_fetch_array($result);
$submenu[] = $result;
}
$response["success"] = 1;
// user node
$response["sub_menu"] = $all_results;
// echoing JSON response
echo json_encode($response);
I'm trying to add some data (strings) from my android app using POST. I'm currently testing the code with the Advanced Rest Client however so the imput's from there.
EDIT: To make it more clear what my question is - what's wrong with the code that it doesn't take the values and assign them? why does it skip the first if?
EDIT 2: I am using chrome.google.com/webstore/detail/advanced-rest-client/… to test this POST.
NOTES:
echo #name; -> it does nothing, doesn't echo a thing
Right now, it automatically enters the third else saying that required field(s) are missing.
I've tried using var_dump on $name and it states the following: Unexpected token s => that didn't make any sense.
Thanks in advance for the answers, if you need more info I'll gladly provide it.
This is the code:
$app->post('/users', function() use ($app, $mysql) {
$name = $app->request->post('name');
$email = $app->request->post('email');
$beer_type = $app->request->post('beer_type');
$favorite_beer = $app->request->post('favorite_beer');
$favorite_drink = $app->request->post('favorite_drink');
echo $name;
if (!empty($name) and !empty($email) and !empty($beer_type) and !empty($favorite_beer) and !empty($favorite_drink)) {
$insert = "INSERT INTO clients(name, email) VALUES({$name}, {$email})";
$getid = "SELECT id_client FROM clients WHERE email = {$email}";
$insert2 = "INSERT INTO preferences(client_id, beer_type, favorite_beer, favorite_drink) VALUES({$idvalue}, {$beer_type}, {$favorite_beer}, {$favorite_drink})";
$request = $mysql->query($insert);
$test = false;
$id = 0;
$test2 = false;
if ($request !== false) {
$test = true;
}
if ($test === true) {
$request = $mysql->query($getid);
if ($request !== false) {
$id = $request->fetch_all(MYSQLI_ASSOC);
}
}
if ($id !== 0) {
$request = $mysql->query($insert2);
if ($request !== false) {
$test2 = true;
}
}
if ($test2 === true) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
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);
}
});
I've solved my issue by changing the java code. I used the Ion Library for the POST and it worked perfectly fine.
In my android app.. a result is taken from server and displayed in app.. where two languages are there for user to select.. English and Hindi.. The english part is working fine and displaying correctly. .but for hindi text ,... ony '???????' is displaying insted of fonts... But when I saved hindi fonts in server ..it is displaying as hindi fonts olnly ... I am using php code for connecting with the server..do we need to change it to utf8.. do we need to change any thing in php file.. I am giving my php code and sql code below.. please check and if there any error pls help..
php
<?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';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
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["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$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);
}
?>
sql code
CREATE TABLE pin1h(
pid int(11) primary key auto_increment,
pinnum int(11),
pinnacle text,
created_at timestamp default now(),
updated_at timestamp
);
Just a tought but i think you should check the encoding on the server to see if what you send is alright, because i had the same problem one time and it turned out the server was sending the bad string.
<?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';
// connecting to db
$db = new DB_CONNECT();
mysql_query("SET NAMES 'utf8'");
// check for post data
if (isset($_GET["pinnum"]))
{
$pinnum = $_GET['pinnum'];
// get a product from products table
$result = mysql_query("SELECT *FROM pin1h WHERE pinnum = $pinnum");
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["pinnum"] = $result["pinnum"];
$product["pinnacle"] = $result["pinnacle"];
$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);
}
?>
I'm using this simple php code to encode the MySql query result in json format.
But Don't know why it is not giving me the desirable output.
I'm actually trying to get the employee details by entering their 'employee_number'.
<?php
require('DB_Connect.php');
require('config.php');
// check for post data
/*
if (isset($_POST["employee_number"])) {
$employee_name = $_POST['employee_number'];
*/
//get a employee from employee_info table
$result = mysql_query("SELECT *FROM employee_info WHERE employee_number =9876543210");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
echo("Success !! Yoo");
$employee = array();
$employee["employee_number"] = $result["employee_number"];
$employee["employee_name"] = $result["employee_name"];
$employee["flag"]=$result["flag"];
// success
$response["success"] = 1;
// user node
$response["employee"] = array();
array_push($response["employee"], $employee);
// echoing JSON response
echo json_encode($response);
} else {
// no employee found
$response["success"] = 0;
$response["message"] = "No employee found";
// echo no users JSON
echo json_encode($response);
}
}else {
// no employee found
$response["success"] = 0;
$response["message"] = "No employee 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);
?>
mysql_query is deprecated. Use mysqli or PDO instead.
make sure you format your query properly *note the spaces
$result = mysql_query("SELECT * FROM employee_info WHERE employee_number = 9876543210");
you could also just echo the $result as your employee array doesn't seem to do anything.
echo json_encode($result);