jsonobjects sudently return empty - php

I was able before to receive json array, now I am receiving empty like this
{"result":[]} this is the url
this is the php code
$con=mysqli_connect($host,$uname,$pwd,$db);
$sql = "select ID,NAME, URL from OBJECTS";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('ID'=>$row[0],
'NAME'=>$row[1],
'URL'=>$row[2]
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
this is my database

NAME is a reserved keyword in MYSQL, I bet you shall change the column name to something like objects_name, then you fetch the query properly. Or you can quote the columns, like :
$sql = "select `ID`,`NAME`, `URL` from `OBJECTS`";

It seems what mysqli_fetch_array($res)) returns empty result.
You can check this: add echo into cycle:
while($row = mysqli_fetch_array($res)) {
print_r($row);
array_push($result,
array(
'ID'=>$row[0],
'NAME'=>$row[1],
'URL'=>$row[2]
));
}
Check also connect with database. Add these two lines before connect to show errors
error_reporting(E_ALL);
ini_set('display_errors', 1);

Related

Remove leading column of zeros when executing a select statement to my MSSQL database

Hey guys i have microsoft sql management studio 18, where I have a database. I'm doing a select statement through php like this:
$conn = OpenCon();
$query = "SELECT id, name, picture, description, numberOfEngines FROM planes";
$result = sqlsrv_query($conn, $query);
if ($result === false) {
$status['status'] = "0";
echo json_encode($status);
}
else{
while($row = sqlsrv_fetch_array($result)) {
$theRows[] = $row;
}
echo json_encode($theRows);
}
CloseCon($conn);
and this is the output:
[{"0":1,"id":1,"1":"114","name":"114","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":2,"id":2,"1":"115","name":"115","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C525","description":"Cessna C525","4":1,"numberOfEngines":1},
{"0":3,"id":3,"1":"124","name":"124","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1},
{"0":4,"id":4,"1":"125","name":"125","2":"airplane1.png","picture":"airplane1.png","3":"Cessna C208B","description":"Cessna C208B","4":1,"numberOfEngines":1}]
How can i remove that leading these duplicates that are showing up twice like the "0":1, or the ariplane.png.
So my output will be like this:
[{"id":1, "name":"114", "picture":"airplane1.png", "description":"Cessna C525", "numberOfEngines":1}]
To return only associative keys in your array, pass SQLSRV_FETCH_ASSOC as the fetchType parameter to sqlsrv_fetch_array:
while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {
This will give you an array with entries like:
{"id":1,"name":"114","picture":"airplane1.png","description":"Cessna C525","numberOfEngines":1}
If you really want the other numeric keys, keep your code as is and add
unset($row[0])
before
$theRows[] = $row;

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 encodes cyryllic as a null

I have MYSQL table with Cyrillic symbols.
This is my MYSQL table
And i use PHP to get MYSQL result and encode it to JSON.
<?php
include 'connection.php';
$array_to_json = array();
$query = "SELECT * FROM online";
$result = mysqli_query($link, $query);
mysqli_set_charset("utf8");
while($row = $result->fetch_assoc()) {
$row_array['parameters'] = $row['parameters'];
$row_array['Descriptions'] = $row['Descriptions'];
$row_array['units'] = $row['units'];
array_push($array_to_json, $row_array);
}
echo json_encode($array_to_json, JSON_UNESCAPED_UNICODE);
$result->close();
?>
And as a result i have got null.
JSON returns null
What do i do wrong?
Did you try using
$row_array['parameters'] = base64_encode($row['parameters']);
$row_array['Descriptions'] = base64_encode($row['Descriptions']);
$row_array['units'] = base64_encode($row['units']);
array_push($array_to_json, $row_array);
recheck your table structure and ensure that there is no extra blank space
for instance:
$row['Descriptions ']
instead of:
$row['Descriptions'];

Im only getting one set of result when trying to use mysql_fetch_array()

function GetVideoInfo( $video_id, $user_id )
{
$result = mysql_query("SELECT * FROM `mytable`
WHERE
video_id = '$video_id'
AND
user_id = '$user_id'")
or die( mysql_error() );
return mysql_fetch_array( $result );
}
$videoRecepients = $viddler_custom->GetVideoRecepients( $video_details['id'] );
echo "<pre>";
print_r($videoRecepients);
echo "</pre>"
When I try using print_r, it only results a single row in the table. My expected result should have 2 results. I am 100% sure that my query is correct, so that is not the problem. I'm thinking that maybe it's on my mysql_fetch_array that is wrong.
Your help would be greatly appreciated and rewarded! Thanks! :)
All of the fetch functions will return a single row, you will need to loop until the result is empty like this (snippet from php.net).
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
printf("ID: %s Name: %s", $row["id"], $row["name"]);
}
From the example on the manual page, mysql_fetch_array returns the information on the current pointer of the $result object. This will mean you want to loop through the result until you've fetched everything.
while ($row=mysql_fetch_array($result)) {
$set[] = $row;
}
print_r($set);
You need to put the mysql_fetch_array($result) in a loop
while($row = mysql_fetch_array($result))
{
// do something with $row
}

Why I am not getting all the result in my query code?

I am having a problem. The following code works fine in my local, but on the live server, it's not working properly..I was supposed to get two rows, but on the live server I am getting only 1 result.
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$result = mysql_fetch_object($query);
print json_encode($result);
What could possibly be the error ?...
I can't believe you get two rows with this, to get all rows you have to do like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
print json_encode($result);
}
mysql_fetch_object/array/row always returns only one row and moves the pointer to the next row, if there is no next row it returns false.
Your code is only getting one row. The mysql_fetch_object() function only returns one row. You need to try something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$json = array();
while ($result = mysql_fetch_object($query))
$json[] = $result;
print json_encode($json);
I think this is because mysql_fetch_object only returns a single row result. You need something like this:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($row = mysql_fetch_array($query))
{
\\access each result here
}
I can't see how you can have two rows in local
$array = array();
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
$array[] = $result;
}
print json_encode($array);

Categories