i am trying to get data from remote mysql database and importing it to json
the connection is ok, the problem is that the result that finally echoed from json is not normal, this is my php code
<?php
require("config.inc.php");
$query = "Select * FROM comments";
$res = mysql_query($query);
$rows = mysql_fetch_assoc($res);
if ($rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($rows as $row) {
$post = array();
$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
and this is the result echoed to the page:
{"success":1,"message":"Post Available!","posts":[{"username":"1","title":"1","message":"1"},{"username":"r","title":"r","message":"r"},{"username":"t","title":"t","message":"t"},{"username":"t","title":"t","message":"t"}]}
while my database table "comments" contain these data:
post_id username title message
1 reda test title 2 test message xxx
2 reda2 title 2 message 2
please help
thanks.
You are only returning 1 row / array by using
$rows = mysql_fetch_assoc($res);
So when you do
foreach ($rows as $row) {
you are trying to get a multidimensional array loop, from a single array. That is why each posts value is the 1st character from each column from the 1 returned row/array.
You need to build your $rows array. Try changing
$rows = mysql_fetch_assoc($res);
to
$rows = array();
while($result = mysql_fetch_assoc($res)){
$rows[] = $result;
}
You're not reading your result set from MySQL correctly. You're fetching just the first row and treating that as the entire results set. You need to loop, retrieving each row in turn.
Try this:
$query = "Select * FROM comments";
$res = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($res)!= 0) {
$response = array();
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
while ($row = mysql_fetch_assoc($res)) {
$post = array();
$post["username"] = $row["username"];
$post["title"] = $row["title"];
$post["message"] = $row["message"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
}
// echoing JSON response
echo json_encode($response);
Note: I haven't tested this code.
2nd Note - you shouldn't be using mysql_*() for new code - it's deprecated. Swicth to mysqli_*() or PDO.
Related
I am designing a PHP service which fetches data for using it in android.
The issue is when I run the query with Where Clause (id < 30) it's working. but when I change (id <40) I am getting a blank screen:
$return_arr = array();
$fe = mysqli_query($conn,"SELECT id, name FROM cable_channels where id < 30;");
while ($row = mysqli_fetch_array($fe)) {
$row_array['id'] = $row['id'];
$row_array['name'] = $row['name'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
<?php
$return_arr = array();
$fe = mysqli_query($conn,"SELECT id, name FROM cable_channels where id < 30;");
while ($row = mysqli_fetch_assoc($fe)) {
$return_arr[] = $row;
}
echo json_encode($return_arr);
?>
Try above code, instead of using
mysql_fetch_array
use
mysql_fetch_assoc
and push all the values to array inside while loop.
I'm trying to list all rows in the database (2 currently) in JSON format, but the only output I'm getting is the last row.
Output:
[{"id":"2","Name":"Googleplex","Address":"1600 Amphitheatre Pkwy, Mountain View, CA","Latitude":"37.421999","Longitude":"-122.083954"}]
Code:
if($status == "connected") {
$locations = $db->prepare('SELECT * FROM Locations');
$locations->execute();
$result = $locations->fetch(PDO::FETCH_ASSOC);
if(!empty($result)) {
// If so, then create a results array and a temporary one
// to hold the data
$resultArray = array();
$tempArray = array();
// Loop through each row in the result set
while($row = $locations->fetch(PDO::FETCH_ASSOC))
{
// Add each row into our results array
$tempArray = $row;
array_push($resultArray, $tempArray);
}
// Finally, encode the array to JSON and output the results
echo json_encode($resultArray);
}
}
The problem is you're throwing away the first result you read in this line:
$result = $locations->fetch(PDO::FETCH_ASSOC);
You can just simplify your code to
if ($status == "connected") {
$locations = $db->prepare('SELECT * FROM Locations');
$locations->execute() or die("Unable to execute query");
$resultArray = array();
while ($row = $locations->fetch(PDO::FETCH_ASSOC)) {
$resultArray[] = $row;
}
echo json_encode($resultArray);
}
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
My table's name is userdetails, it has four attributes named name, username, mobile and password. I want to get all the mobile numbers and store it in an array using php.
I have used the following php code
if($_SERVER['REQUEST_METHOD']=='GET'){
require_once('dbConnect.php');
$mobile = $_GET['mobile'];
$sql = "SELECT MOBILE FROM USERDETAILS";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"MOBILE"=>$res['MOBILE']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
but all I am getting is the first entry of the database.
Please help.
You should loop through the records by doing the following:
$result = [];
while ($array = mysqli_fetch_array($r)) {
$result[] = $array['MOBILE'];
}
echo json_encode($result);
For getting all number of column use while loop as
$jsonData = array();// initialized you array
while ($array = mysqli_fetch_array($r,MYSQLI_ASSOC)) {// add MYSQLI_ASSOC to get associative array
$jsonData[] = $res['MOBILE'];// store data into array
}
echo json_encode($jsonData);// convert in into json
Try this:
$result = mysqli_query($con,$sql);
$mob = Array();
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$mob[] = $row['MOBILE'];
}
I am trying to pull pictures from a table row that are base64 encoded. I need to decode them and display on a webpage. I would really like to put them into a slideshow but that is another topic!
Here is the query so far
<?php
require("config.inc.php");
//initial query
// table name is pictures and table row is picture
$query = "Select * FROM pictures";
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
// Finally, we can retrieve all of the found rows into an array using fetchAll
$rows = $stmt->fetchAll();
if ($rows) {
$response["success"] = 1;
$response["message"] = "Photos Available!";
$response["posts"] = array();
// only 3 rows in table - post_id, username, picture
foreach ($rows as $row) {
$post = array();
$post["post_id"] = $row["post_id"];
$post["username"] = $row["username"];
$post["picture"] = $row["picture"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Photos Available!";
die(json_encode($response));
}
?>
The problem is decoding it. Here is what it shows so far
http://www.photosfromfriends.com/webservice/gallery.php
This is only for one picture (post) The table might have 50 pictures in it and each will need to be displayed (hence the desire for a slideshow). This is way over my head and I would really appreciate any help.
try this code, please justify according to your code:
$data = json_decode($json, true);
//print_r($data);
$picture = base64_decode($data['posts'][0]['picture']);
header("Content-type: image/png");
echo $picture;
You must decode only decoded data for the image, and don't forget to use header