PHP MYSQL Json Issue - php

We have a simple php mysql json api to provide data to android app
We have a fix categories.
so we just check if category is this then fetch result from mysql database and convert in json
Everything is working fine. Fetching url from mysql
$stm_row = $stm->fetchAll(PDO::FETCH_ASSOC);
Till here all is working fine. data is coming from every category
Then, we convert data to JSON:
print(json_encode($stm_row));
But the problem is that only 1 catgory data is printing using json, rest category data showing blank
Can you please check what can be issue

If you want to loop through results ...e.g.
$stm_row = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($stm_row as $row => $cat) {
echo $cat['category_name'];
}
OR If you have encoded JSON, need to decode first which will return an array...which you can loop through like ...e.g.
$stm_row = json_encode($stm_row);
$arr = json_decode($stm_row, true);
foreach ($arr as $row => $cat) {
echo $cat['category_name'];
}

i am giving you simple demonstration how to work with json.you can try like that
$data=array();
while($row = mysqli_fetch_array($query)) {
$data[] = $row['your field name'];
}
echo json_encode(array("response"=>$data));

Related

How to get the array object value in php?

I am new to php, for my android application I am using php. My problem is I can not able to access the array object value.
Store the select query table value like below
$rows = array();
$i=0;
while($r = mysqli_fetch_assoc($result)) {
$rows[$i] = $r;
$i++;
}
When I display the value using echo json_encode($rows); it will display [{"CurrentVersion":"0.0.1","ForceUpdate":"1"}] this value. But when I trying to access this value in php I can not able to access I tried the below methods.
echo $verDetails[0].ForceUpdate; => ArrayForceUpdate <- I got this value
echo $verDetails[0] => Array <- I got this value
echo $verDetails[0]->ForceUpdate;=> Nothing got
I want to get the Forceupdate value = 1. Please help me, I know many one feel this is the cheap question and you will put negative vote, but I don't have other option. I tried in many ways but I am not get any answers.
mysqli_fetch_assoc returns an associative array. So you have to access the values like this:
$verDetails[0]['ForceUpdate']
To use the arrow syntax, use mysqli_fetch_object:
while($r = mysqli_fetch_object($result)) {
$rows[$i] = $r;
$i++;
}
You have multidimensional array try like this..
echo $rows[0]['CurrentVersion'];
echo $rows[0]['ForceUpdate'];
If you have json format data.use json_decode() to convert it into array.Then access each element.like this
$json = '[{"CurrentVersion":"0.0.1","ForceUpdate":"1"}]';
$rows = json_decode($json,true);
echo $rows[0]['CurrentVersion']."<br/>";
echo $rows[0]['ForceUpdate'];

cant fetch image with mysql_fetch_object when fetching data from SQL

i have done this code in php and it fetches every detail correctly apart from the images it shows "product_photo":null while there is an image.
any ideas why is null?
while ($row = mysql_fetch_object($qry_result)){
$result[] = $row;
}
$jsonstring = json_encode($result);
echo $jsonstring;

PHP get string contain url from database

I'm trying to get string from database which is url "http://www.google.com/"
but data I get changed to this http:\ /\ /www.google.com\ /
while($row = mysql_fetch_array($result)){
// temporary array to create single category
$tmp = array();
$tmp["id"] = $row["id"];
$tmp["name"] = $row["name"];
$tmp["url"]= $row["url"];
array_push($response["database"], $tmp);
}
how can I get the url without changed.
In your example the two pieces of data are identical, did StackOverflow reformat your data? Your code looks fine to me, perhaps it is a problem with how the data is inserted into the database rather than how it is retrieved. Have you looked at the data in an SQL browser like phpMyAdmin or SQLyog Community Edition to confirm the data is stored as you expect?
the stripslashes() builtin function would seem to do the trick.
I solve this problem
actually I want to create .json file, but when we use array and show data like url in php page .php the data will change because it's read as an html code, in json case we have to create another file with in file managment generated after getting data from database
$response = array(); $response["feed"] = array();
foreach($db->query('SELECT * FROM table') as $row) {
$tmp = array();
$tmp['id'] = $row['id'];
$tmp['name'] = $row['name'];
$tmp['url']= $row['url'];
array_push($response['table'], $tmp); }
//here the data posted into php page so the url change
echo json_encode($response);
//here create .json data and write data in data.json
$fp = fopen('data.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);

Disappear the arrays generated with mysql_fetch_array() after use?

I have a typical database query:
$query = mysql_query('SELECT titulo,referencia FROM cursos WHERE tipo=1 AND estado=1');
and I can convert it in an array and print the data:
while ($results=mysql_fetch_array($query)): ?>
echo $results['referencia'];
// and so...
endwhile;
but in some cases I need to print the same data in another part of the web page, but the $results array seems to be empty (I use var_dump($results) and I get bool(false)). I plan to use what I learned reading Create PHP array from MySQL column, but not supposed to mysql_fetch_array() creates an array? So, what happen?
Tae, the reason that your $result is false at the end of the while loop is that mysql_fetch_array returns false when it reaches the end of the query set. (See the PHP Docs on the subject) When you reach the end of the query set $results is set to false and the while loop is exited. If you want to save the arrays (database row results) for later, then do as Chacha102 suggests and store each row as it is pulled from the database.
$data = array();
while($results = mysql_fetch_array($query)) {
$data[] = $results;
}
foreach ($data as $result_row) {
echo $result_row['referencia'];
... etc.
}
Try this
while($results = mysql_fetch_array($query))
{
$data[] = $results;
}
Now, all of your results are in $data, and you can do whatever you want from there.
As Anthony said, you might want to make sure that data is actually being retrieved from the query. Check if any results are being returned by echo mysql_num_rows($query). That should give you the number of rows you got

Adding items to a JSON encoded array

I am using this solution found on stackoverflow to encode my MYSQL output to a JSON encoded array.
$sth = mysql_query("SELECT ...");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
This works great and produces an output of
[{"id":"81","title":"Something Here","start":"2009-10-27 09:00:00"},{"id":"77","title":"Report on water","start":"2009-10-30 09:00:00"}]
Now I need to put a value of say
"colour":"Blue"
within the JSON encoded array.
So i need the ouput to look like
[{"id":"81","title":"Community Awareness","start":"2009-10-27 09:00:00", "colour":"Blue"},{"id":"77","title":"Write a 10,000 Page Report on Emma","start":"2009-10-30 09:00:00", "colour":"Blue"}]
Does anyone have any solutions on how I could achieve this?
Thanks,
Tim Mohr
Before you call json_encode($rows), just edit the value in the $rows array:
$rows[0]['colour'] = 'Blue'; // changes the colour of the first row in the array
edit in fact, if you just want to add a colour to all of the rows, you can do a simple foreach:
foreach ($rows as &$row) {
$row['colour'] = 'Blue';
}

Categories