PHP JSON not displaying MySQL result - php

The PHP I created retrieve data from a MySQL database and turns it into a JSON which is then echoed. I had 300 records and the PHP was able to display the JSON and was visible when viewed.
However, I added another 100 records to the same table and for some reason the JSON isn't being displayed. It just appears blank with no error. But when I remove the 100 records, the JSON displays as normal.
I haven't touched the PHP file during this. What could the reason be?
<?PHP
include_once("connection.php");
$query = "select id,mosque_name,latitude,longitude from mosques;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
$response["mosques"] = array();
while ($row = mysqli_fetch_array($result)) {
$mosque = array();
$mosque["id"] = $row["id"];
$mosque["mosque_name"] = $row["mosque_name"];
$mosque["latitude"] = $row["latitude"];
$mosque["longitude"] = $row["longitude"];
array_push($response["mosques"], $mosque);
}
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No mosques found";
echo json_encode($response);
}
?>

Try This:-
<?PHP
include_once("connection.php");
$query = "select id,mosque_name,latitude,longitude from mosques;";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) == 0) {
echo json_encode("");
}
else{
while ($row = mysqli_fetch_array($result)) {
/*$mosque = array();
$mosque["id"] = $row["id"];
$mosque["mosque_name"] = $row["mosque_name"];
$mosque["latitude"] = $row["latitude"];
$mosque["longitude"] = $row["longitude"];*/
$fetchRow[]=$row;
// array_push($response["mosques"], $mosque);
}
echo json_encode($fetchRow);
}
/* $response["success"] = 1;
echoing JSON response
echo json_encode($response);*/
?>
and check your result from network

Related

How to format a json response in php

I am new to php and am trying to return a json response in a particular structure. Here is what I have tried so far:
$response = array();
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branchId'] = $row['branchId'];
$response[$x]['branch'] = $row['branch'];
$response[$x]['customerGroupId'] = $row['customerGroupId'];
$response[$x]['customerGroup'] = $row['customerGroup'];
$response[$x]['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}
The code above returns this response:
However, instead of returning for example "branchId" and "branch" as individual properties, I want to pass their values inside a branchObject such that branch.id == "branchId" and branch.name == "branch".I mean, How may I return the response in the following structure:
And Here is how my database looks like:
How can I achieve this?
You ask for stuff that we are unsure if db result returns but as nice_dev pointed out, you need something like this:
$response = [];
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branch']['id'] = $row['branchId'];
$response[$x]['branch']['name'] = $row['branch'];
$response[$x]['customerGroup']['id'] = $row['customerGroupId'];
$response[$x]['customerGroup']['name'] = $row['customerGroup'];
$response[$x]['customerGroup']['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}

JSON API in PHP

My android developer require JSON response like below.
[
{ "StudentId":"1", "StudentName":"Rahul", "StudentMarks":"83" }, { "StudentId":"2", "StudentName":"Rohit", "StudentMarks":"91"} ]
My current code for generate JSON API is like below.
<?php
$response = array();
require 'db.php';
$query = "SELECT * FROM news order by id desc";
$result = mysqli_query($conn,$query);
if (mysqli_num_rows($result) > 0) {
$response["news"] = array();
while ($row = $result->fetch_assoc()) {
$news= array();
$news["id"] = $row["id"];
$news["title"] = $row["title"];
$news["description"] = $row["description"];
array_push($response["news"], $news);
}
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
echo json_encode($response);
}
I am getting response like below
{"news":[{"id":"1","title":"My first news title here","description":"My first news description will be here"}],"success":1}
I have tried lot of changes but not getting proper result like above. I want remove "news" and "success" from my response for make it as required response. What should I change for get response like first located code ?
Thanks
Try this code
<?php
$response = array();
require 'db.php';
$query = "SELECT * FROM news order by id desc";
$result = mysqli_query($conn,$query);
if (mysqli_num_rows($result) > 0) {
$response["news"] = array();
while ($row = $result->fetch_assoc()) {
$arr[]=array('StudentId'=>$row["id"],'title'=>$row["title"],'description'=>$row["description"])
}
$response =$arr ;
// echoing JSON response
echo json_encode($response);
} else {
$response = array('success'=>0);
echo json_encode($response);
}

How can i get list of news from database with comments

i'm going to make news app for android. i made json_encode to my php file. but i just want to add comments in while. i was search it everywhere but i coulnt find. can you please help me.
Here is my php code
if (!empty($result)) {
// check for empty result
if ($result->num_rows > 0) {
$response["success"] = true;
$response["news"] = array();
while($row = $result->fetch_object()){
$news = array();
$news["id"] = $row->id;
$news["title"] = $row->title;
$news["details"] = $row->short;
$news["thumbURL"] = $thumb.$row->images;
$news["LargeImageURL"] = $big.$row->images;
array_push($response["news"], $news);
}
// echoing JSON response
echo json_encode($response);
} else {
// no news found
$response["success"] = false;
$response["message"] = "No news found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no news found
$response["success"] = false;
$response["message"] = "No news found";
// echo no users JSON
echo json_encode($response);
}
and i want to add item's comments like in photo
http://i.stack.imgur.com/RnaOJ.jpg
I found and its work good. Thanks for your helping.
if (!empty($result)) {
// check for empty result
if ($result->num_rows > 0) {
$response["success"] = true;
$response["news"] = array();
while($row = $result->fetch_object()){
$commentsql = $db->query("SELECT * FROM comment where cid='".$row->id."' order by id desc limit 0,10");
$comments = array();
while($comm = $commentsql->fetch_object()){
$comments[] = array(
'id' => $comm->id,
'text' => $comm->comment
);
}
$news = array();
$news["id"] = $row->id;
$news["title"] = $row->title;
$news["details"] = $row->short;
$news["thumbURL"] = $thumb.$row->images;
$news["LargeImageURL"] = $big.$row->images;
$news["comment"] = $comments;
array_push($response["news"], $news);
}
// echoing JSON response
echo json_encode($response);
} else {
// no news found
$response["success"] = false;
$response["message"] = "No news found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no news found
$response["success"] = false;
$response["message"] = "No news found";
// echo no users JSON
echo json_encode($response);
}

MySQL query returns null results when used in php

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.

Return blob of pdf link as JSON for android

I know that there are similar questions around regarding SQL Blob and PHP JSON but I do not know how to return the SQL Blob as a JSON according to my codes.
I am uploading a pdf from the website to a database and I want to view this pdf on an android but as you can see, the data -the link/blob- is null.
This is what was returned to me:
{"file":[{"id":"1","batchid":"121001","name":"dmxsuits.pdf","mime":"application/pdf","size":"263883","data":null,"created":"2013-08-05 03:29:38"}],"success":1}
My php code:
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("SELECT * FROM file ORDER BY batchid DESC") or die(mysql_error());
if (mysql_num_rows($result)>0) {
$response["file"] = array();
while ($row= mysql_fetch_array($result)) {
//temp user array
$pdfFiles = array ();
$pdfFiles['id'] = $row['id'];
$pdfFiles['batchid'] = $row['batchid'];
$pdfFiles['name'] = $row['name'];
$pdfFiles['mime'] = $row["mime"];
$pdfFiles['size'] = $row["size"];
$pdfFiles['data'] = $row['data'];
$pdfFiles['created'] = $row['created'];
array_push($response["file"], $pdfFiles);
}
//success
$response["success"] = 1;
//echoing JSON response
echo json_encode($response);
} else {
$response["success"] =0;
$response["message"] = "No pdf found";
echo json_encode($response);
}
?>

Categories