Unable to get data in proper format - php

I am unable to parse data in PHP from MySQL.
Here's my code
<?php
header('Content-Type: text/html; charset=utf-8');
// array for JSON response
$response = array();
echo 'भगवान';
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT *FROM create_event") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["create_event"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["id"] = $row["id"];
$product["desc"] = $row["desc"];
$text;
array_push($response["create_event"], $product);
}
// success
$response["success"] = 1;
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
It's my database, I have set all the collation in utf-8 ci format but still not working:
I have tried all possible solutions and help online and have gone through popular posts and answers and also have set browsers settings to support Hindi lang but still displaying ? marks in output. Here is the output format
भगवान{"id":"1","desc":"???? ??????"}

mysql_query('SET character_set_results=utf8');
Use this after connecting to database or before using any select statement.

Related

CRUD Read operation PHP

I am creating an Android Application that requires information to be retrieved from a MySQL database on a MAMP server. I have written PHP code to try retrieve the information from the database however there is no information being retrieved from the database. I have checked the code using PHP code checker and there is no issues found. Can anyone find any issues or provide any links to help. Thank you in advance.
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once('connect.php');
// connecting to db
//$db = new db_name();
// get all products from products table
$result = mysqli_query("SELECT * FROM tbl_book");
// check for empty result
if (mysqli_num_rows($result) > 0) {
// looping through all results
// book node
$response["books"] = array();
while ($row = mysqli_fetch_array($result)) {
// temp user array
$book = array();
$book["id"] = $row["id"];
$book["title"] = $row["title"];
$book["description"] = $row["description"];
$book["bookID"] = $row["bookID"];
// push single book into final response array
array_push($response["books"], $book);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No book found";
// echo no users JSON
echo json_encode($response);
}
?>

Android: Get details from php script with htaccess and htpasswd

I have a php script with this output:
And then I display this data in Android. But now, I put a .htaccess and .htpasswd file in the directory and I have to enter a password, if I want to read the data. My question is how can I display the data in Android, now with the files (.htaccess and .htpasswd). Is there a way to enter the password in Android to show the data because now I cant display the data..
Thanks
EDIT:
This is my php script:
<?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 users") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["feed"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$feed = array();
$feed["uid"] = $row["uid"];
$feed["unique_id"] = $row["unique_id"];
$feed["name"] = $row["name"];
$feed["full_name"] = $row["full_name"];
$feed["email"] = $row["email"];
$feed["age"] = $row["age"];
$feed["about_the_customer"] = $row["about_the_customer"];
$feed["encrypted_password"] = $row["encrypted_password"];
$feed["salt"] = $row["salt"];
$feed["created_at"] = strtotime($row["created_at"]);
$feed["updated_at"] = $row["updated_at"];
$feed["imageName"] = $row["imageName"];
// push single product into final response array
array_push($response["feed"], $feed);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No feed found";
// echo no users JSON
echo json_encode($response);
}
?>

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

Android hindi font not displaying

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

testing php function with poster extention

I made a simple API to get the data from the database to be viewd at android app
I have a Problem testing my php functions
example of the functions:-
<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];
// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["city"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
// Full texts id dead_name masged_name gender salah notes date city_name hour min
$region_subscribt = array();
$region_subscribt["id"] = $row["id"];
$region_subscribt["city_name"] = $row["city_name"];
$region_subscribt["region_name"] = $row["notes"];
$region_subscribt["notes"] = $row["country_name"];
// push single product into final response array
array_push($response["city"], $region_subscribt);
}
// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;
// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function
// get_city();
?>
when I using poster extention with firefox and put at the content , I got no data
when I put at the content
getcity();
but when I call the function inside the php file I got the data correctly !
How to pass the values at the httprequest to use the returned json at another app !
You need to send the method name as a parameter not as a content and then check the method name and call the code you need.
see this snapshot
if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
get_city();
}
you also need to set the content type as following
header('Content-type: application/json');
your final code will be something like this
<?php
include 'json_config.php';
function get_city(){
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
$return=array();
//$country_name=$_POST['country_name'];
// get all products from products table
$result = mysql_query("SELECT *FROM city") or die(mysql_error());
header('Content-type: application/json');
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["city"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
// Full texts id dead_name masged_name gender salah notes date city_name hour min
$region_subscribt = array();
$region_subscribt["id"] = $row["id"];
$region_subscribt["city_name"] = $row["city_name"];
$region_subscribt["region_name"] = $row["notes"];
$region_subscribt["notes"] = $row["country_name"];
// push single product into final response array
array_push($response["city"], $region_subscribt);
}
// echoing JSON response
echo json_encode($response,JSON_UNESCAPED_UNICODE);
} else {
// no products found
$response["fail"] = 0;
// echo no users JSON
echo json_encode($response,JSON_UNESCAPED_UNICODE);
}
}//end of the function
if(isset($_GET['method_name']) && $_GET['method_name']) == 'get_city') {
get_city(); }
?>
and here is some very good and pretty easy tutorial summarize the whole precess
Create a Basic Web Service Using PHP, MySQL, XML, and JSON

Categories