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
Related
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);
}
?>
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);
}
?>
I want to send database records with a PHPH file via json to my app I am making with IntelXDK. Because I can't use PHP code with the Intel XDK, I needed to use JSON. I want to show the two records 'quote' and 'author' from my 'quotes' table on my screen. Someone helped me to this code but it just returns [null,null]instead of the two records I need.. I tried debugging but I am new to PHP so I can'get it to work.. Anyone who can help or sees an error in this code? Thanks!
PS: Yes I now there are already multiple questions asked on this subject by other people. I have read them all but none of them solves my question..
<?php
if(isset($_GET["get_rows"]))
{
//checks the format client wants
if($_GET["get_rows"] == "json")
{
$link = mysqli_connect("localhost", "xxxxx", "xxxxx", "xxxx");
/* check connection */
if (mysqli_connect_errno()) {
echo mysqli_connect_error();
header("HTTP/1.0 500 Internal Server Error");
exit();
}
$query = "SELECT quote, author FROM quotes WHERE id = " . date('d');
$jsonData = array();
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
$row = $result->fetch_assoc($result);
// Create a new array and assign the column values to it
// You can either turn an associative array or basic array
$ret= array();
$ret[] = $row['quote'];
$ret[] = $row['author'];
//encode to JSON format
echo json_encode($ret);
}
else {
echo json_encode($ret);
}
/* close connection */
mysqli_close($link);
}
else
{
header("HTTP/1.0 404 Not Found");
}
}
else
{
header("HTTP/1.0 404 Not Found");
}
?>
You have a bug in fetch_assoc() function call - remove $result parameter. If you had error reporting enabling, you should see:
Warning: mysqli_result::fetch_assoc() expects exactly 0 parameters, 1 given
Just change it to:
$row = $result->fetch_assoc();
In javascript to parse this response, just do this:
var obj = JSON.parse(xmlhttp.responseText);
document.getElementById("quote").innerHTML = obj[0];
document.getElementById("author").innerHTML = obj[1];
I think your problem is with fetch_assoc()
Try to use that :
$row = mysqli_fetch_assoc($result);
instead of
$row = $result->fetch_assoc($result);
It's works for me with your example
change this
$row = $result->fetch_assoc($result);
to
$row = $result->fetch_assoc();
Just change it to:
$row = $result->fetch_assoc();
Updated:
response = JSON.parse(xmlhttp.responseText);
you can now access them independently as:
reponse.quote and response.author
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.
I'm using php to retrieve long text files from a mysql database. These files are pdfs encoded into base64 strings. The strings work perfectly fine in the database and produce the pdf I desire, but when I retrieve the data through php there is always a large block of characters missing in the middle of the string.
I've tried this with multiple different strings and it keeps giving me the same result, a large block of characters missing in the middle of the string. Any help would be appreciated.
Here is the code I'm using, my mistake. At the moment I'm just printing the string onto the screen.
<?php
// Make sure an ID was passed
if(isset($_GET['id'])) {
// Get the ID
$id = intval($_GET['id']);
// Make sure the ID is in fact a valid ID
if($id <= 0) {
die('The ID is invalid!');
}
else {
// Connect to the database
$dbLink = new mysqli('***', '***', '***', '***');
if(mysqli_connect_errno()) {
die("MySQL connection failed: ". mysqli_connect_error());
}
// Fetch the file information
$query = "SELECT name, data FROM report_storage WHERE `id` = {$id}";
$result = $dbLink->query($query);
if($result) {
// Make sure the result is valid
if($result->num_rows == 1) {
// Get the row
$row = mysqli_fetch_assoc($result);
// Print headers
// Print data
$data = $row['data'];
//echo strlen($data);
echo $data;
}
else {
echo 'Error! No image exists with that ID.';
}
// Free the mysqli resources
#mysqli_free_result($result);
}
else {
echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
}
#mysqli_close($dbLink);
}
}
else {
echo 'Error! No ID was passed.';
}
?>