Android hindi font not displaying - php

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);
}
?>

Related

Android PHP Mysql get Data by params

I'm making android app with communication to PHP MySQL
there is PHP files
create_order.php
get_all_orders.php
get_order_status.php
create_order.php works
get_all_orders.php also
but example in browser I'm typing get_order_status.php?oid=102 it gives me response but it contains an error
{"success":0,"message":"Required field(s) is missing get status"} when trying it from android by POST method
RequestQueue queue = MyVolley.getRequestQueue();
String num1 = "1";
if (num1 != null && !num1.equals("")) {
String uri = String
.format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",
num1);
StringRequest myReq = new StringRequest(Request.Method.GET,
uri,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
get_order_status.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
//echo("post data - ".$_GET['oid']);
if (isset($_GET['oid'])) {
$pid = $_GET['oid'];
// get a order from orders table
$mysqli = new mysqli('localhost','root','rootpass','testdb');
$result = mysqli_fetch_array($mysqli->query("SELECT * FROM orders WHERE oid = $pid"));
//echo($result["status"]);
if (count($result)>0) {
// check for empty result
$order = array();
$order ["status"] = $result["status"];
// success
$response["success"] = 1;
// user node
$response["order"] = array();
array_push($response["order"], $order);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No order found";
// echo no users JSON
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing get status";
// echoing JSON response
echo json_encode($response);
}
?>
change param1 to oid in this line (android code)
String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?param1=%1$s",num1);
should be like this:
String uri = String.format("http://192.168.0.101/android_connect/get_order_status.php?oid=%1$s",num1);
as in your PHP file you are reading parameter named oid
$pid = $_GET['oid'];

php login screen error

<?php
/*
* Following code will update a product information
* A product is identified by product id (pid)
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['email']) && isset($_POST['pass'])) {
$eml = $_POST['email'];
$pass = $_POST['pass'];
$e = "$eml";
// include db connect class
// connecting to db
$db = new PDO('mysql:host=localhost;dbname=quotes;charset=utf8', 'Sidd');
// mysql update row with matched pid
$result = $db->query("SELECT * FROM user WHERE email='$e'");
// check if row inserted or not
if ($result) {
$response["success"] = 1;
$response["message"] = "Login Successful";
// while($row = $result->fetch(PDO::FETCH_ASSOC)) {
// $response["uid"] = $row["uid"];
// 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);
}
?>
Error
Android app always return Success=1 and login successful whether the entered Email id is right or wrong! As such whether email id exist in database or not. what am i missing? :/ .................... :(
And Also inform whether the commented code will work?!!
Please help
replace
$result = $db->query("SELECT * FROM user WHERE email='$e'");
if ($result) {....}
with
$result = $db->query("SELECT * FROM user WHERE email='$e'");
$row_count = $result->rowCount(); //number of rows with same email
if($row_count >0) {...}
PDO query method returns object. Object is always compared to true. ALWAYS.
query method can return false but only in case there is an error. But having no values that corresponds to given email is not an error.

How do i add register validation into this PHP?

How do i add register validation so that user wont be able to use the same user id as others? I'm stuck here as I have tried every code and nothing would work. And it will crash my app.
I'm using Eclipse to do my app.
My php file
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['user_name']) && isset($_POST['user_pwd'])){
$user_name = $_POST['user_name'];
$user_pwd = $_POST['user_pwd'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");
// check if row inserted or not
if ($result) {
// 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);
}
change this part in your code
// connecting to db
$db = new DB_CONNECT();
// chack database
$result = mysql_query("select user_name from doc_user where user_name = '$user_name'");
if(mysql_num_rows($result)){
$response["success"] = 0;
$response["message"] = "username not available.";
// echoing JSON response
echo json_encode($response);
exit;
}
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");

Php web service to get data in json format

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);

mysql query and encoding it in json format

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);

Categories