cant fetch image with mysql_fetch_object when fetching data from SQL - php

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;

Related

PHP MYSQL Json Issue

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

Half of data lost on JSON conversion?

I have a mysql database of countries, 250 in total, which in want to give over to an Android app. I know i have to use php in between to parse the result into JSON. This is it:
<?php
require_once('connection.php');
$response = array();
$resultarray = array();
$result = mysqli_query($con, "SELECT * FROM countries");
if (!empty($result)) {
// check for empty result
while ($row=mysqli_fetch_assoc($result)) {
print $row;
$resultarray = mysqli_fetch_array($result);
$Laender = array();
$Countries[de] = $resultarray["de"];
$response["Countries"] = array();
array_push($response["Countries"], $Countries);
echo json_encode($response);
}
}
?>
I ran the script in my browser and it displays correctly, except that half the countries are missing. There are only 125 countries displayed. Where have they vanished to?
Both functions mysqli_fetch_assoc and mysqli_fetch_array do the same - they fetch next record.
So, in your while you fetch first record with mysqli_fetch_assoc and then immediately fetch second record with mysqli_fetch_array. So, the first record is lost. And this happens on every iteration, so half of your records are lost.
Get rid of mysqli_fetch_array call:
$response["Countries"] = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($response["Countries"], $row["de"]);
}
echo json_encode($response);

Having trouble while encoding json Array in php

I have two different queries which I have to append in same array in form of JSON..
Here is my code from 1st query...
while($row = mysqli_fetch_array($res)) {
array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}
Here is my second query push similar as first one.. number of rows is always same as above query
array_push($result,array('status'=>'$status');
After that I'm encoding them like this
echo json_encode(array("result"=>$result));
Here is what I am getting
{"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26"},
{"status":"status"}]
But I want to result like this
{"result":[{"name":"Abhishek Singh","photo":"http:\/\/onsitesupport.info\/diary\/photos\/student\/26.png","rollno":"1","id":"26","status":"status"}]
I mean status will merge into my every node... how can I achieve this..?
Try the below to add status field to each array:
while($row = mysqli_fetch_array($res)){
array_push($result,array('name'=>$row[0],'photo'=>$row[1],'rollno'=>$row[2],'id'=>$row[3]));
}
$res_row = 0;
while($row2 = mysqli_fetch_array($res2)){
$status = $row2[0]; // status value here
$result[$res_row]['status']=$status;
$res_row++;
}
echo json_encode(array("result"=>$result));
Try this please, using temporary arrays that are merged after all your queries are complete:
// Query 1
while($row = mysqli_fetch_array($res)){
$tmpResults[] = $row;
}
// Query 2
$tmpResult2 = array('status'=>'$status');
// Merge Everything
$final = array_merge($tmpResults, $tmpResult2);
// Encode
$json = json_encode($final, TRUE);
Good luck

JSON and array structure typeahead

I am trying to create a json file from an sql query, and search this json using twitter typeahead. However the json format doesn't look correct.
The json needs to be in a certain format for typeahead like below;
['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California' ...];
However my json is in the following format;
["{\"title\":\"Item 1\"}","{\"title\":\"Item 2\"}","{\"title\":\"Item 3\"}"
Newbie to php/sql/json I'm sure there is something really obvious I'm missing or doing wrong. Maybe I should be using a foreach and not while? I am able to echo out the $titles so I now the query is working.
If somebody cold point me in the right direction I would appreciate it.
My code so far;
$sql = ("SELECT title FROM publication");
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
$data = array();
while($row = $result->fetch_assoc()){
$data[] = json_encode($row);
$titles = json_encode($data);
echo $titles;//for testing
}
file_put_contents('titles.json', $titles);
You're over-encoding your data and
you're not including the data you actually want.
Put the data you want into the array and JSON-encode the whole thing only at the end:
while ($row = $result->fetch_assoc()) {
$data[] = $row['title'];
}
file_put_contents('titles.json', json_encode($data));
You are performing json_encode twice which should not be the case.
Instead the Code should be like below:
$data[] = $row;
$titles = json_encode($data);
or simply
$titles = json_encode($row);
Use json_encode once
$data[] = $row; /*$data[] = json_encode($row);*/
and write this:
$titles = json_encode($data);
OR
$titles = json_encode(array_values($data)); /*You may need to use this to get exact output*/
After while loop
You are inserting associative array to your json array('title'=>'sometitle') but you only need that title.
The solution is to only save the title value from the db resulting row to the array:
while($row = $result->fetch_assoc()){
$data[] = $row['title'];
echo json_encode($data); // dont encode twice
}
file_put_contents('titles.json', json_encode($data));

Json encode error from mysql result

I have done a little research on the following error Warning: [json] (php_json_encode) type is unsupported, encoded as nullbut have not found much in the way of answers?
I am trying to encode the result of a mysql query. Not sure what info I can provide.. but when I echo the results of the mysql data using
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
{
$content[] = $info;
}
$count = count($content);
for($i=0;$i<$count;$i++){
echo $content[$i]['Name'];
}
every thing shows as normal. Any help would be great.
What about:
$result=array();
for($i=0;$i<$count;$i++)
{
$result[]=$content[$i]['Name'];
}
echo json_encode($result);
mysql_fetch_array will return an array with both numeric and non-numeric values. That's fine for PHP, but it doesn't really play as well in other formats and it could very well be the cause of the issue. Have you tried mysql_fetch_assoc?
BIG EDIT
Just noticed your code above. You have :
$data = json_encode($result);
echo $data;
while($info = mysql_fetch_array($result))
That won't work. $result is a resource data type. It can't be serialized into something which can be read by json_encode. You have to create an array of its contents first and serialize that. Just making sure you know.

Categories