PHP data Loading Issue - php

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.

Related

Get an array of JSON data from php database

I am kinda new to SQL and I am having a problem. I am following the tutorial from Here to try to get data from my database into my android app.
I have it working, but it only gets one line. I know it should be possible to iterate through the database and echo out every line that matches the ID to JSON, but I do not know how. How would I do this?
My current code:
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('dbConnect.php');
$sql = "SELECT * FROM LATLON WHERE DeviceID='".$id."'";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"INDEX"=>$res['INDEX'],
"DeviceID"=>$res['DeviceID'],
"LAT"=>$res['LAT'],
"LON"=>$res['LON']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
?>
EDIT
I edited my code to this, but I am getting nothing. Still not quite understanding what is happening. Thanks for all the help!
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$id = $_GET['id'];
require_once('dbConnect.php');
$sql = "SELECT * FROM LATLON WHERE DeviceID='".$id."'";
$r = mysqli_query($con,$sql);
while($row = $result->mysqli_fetch_array($r, MYSQLI_ASSOC)){
$array[] = $row;
}
echo json_encode($array);
mysqli_close($con);
}
?>
You can iterate through mysqli_fetch_array();
while($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$array[] = $row;
}
echo json_encode($array);
When you use the OO interface, the method names don't begin with mysqli_, that's only used for procedural calls. You also have no variable named $result, the result is in $r.
So it should be:
while ($row = $r->fetch_array(MYSQLI_ASSOC)) {
or:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

Printing multiple table queries using json_encode in php

I need to print multiple database queries using json_encode(). I have this code below that outputs database records using json_encode. This works fine.
<?php
error_reporting(0);
include('db.php');
$result = $db->prepare('SELECT username,firstname,lastname FROM user');
$result->execute(array());
$data = array();
while ($row = $result->fetch()) {
$data[] = $row;
}
echo json_encode($data);
?>
I need to add another database query so that I can print them together using the same json_encode. Here is the database query that I want to add so that I can print them together using json_encode:
<?php
include('db.php');
$result = $db->prepare('SELECT price AS price1,goods AS product FROM provision_table');
$result->execute(array());
$data_pro = array();
while ($row = $result->fetch()) {
$data_pro[] = $row;
}
echo json_encode($data_pro);
?>
How can I accomplish this?
I think this might hit what you're looking for.
<?php
error_reporting(0);
include('db.php');
$result = $db->prepare('select username,firstname,lastname from user');
$result->execute(array());
$data = array();
while ($row = $result->fetch()) {
$data[] = $row;
}
$result = $db->prepare('select price as price1,goods as product from provision_table');
$result->execute(array());
$data_pro = array();
while ($row = $result->fetch()) {
$data_pro[] = $row;
}
// Combine both arrays in a new variable
$all_data['user'] = $data;
$all_data['pro'] = $data_pro;
echo json_encode($all_data);

How to get entire column from database and store it in an array using php?

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'];
}

Convert mysql select results to json (array of arrays)

I need to encode a table content to JSON in order to insert it into a file.
The output has to be as following :
{
"name1":[{"id":"11","name":"name1","k1":"foo","k2":"bar"}],
"name2":[{"id":"12","name":"name2","k1":"foo","k2":"bar"}],
}
Indeed, each JSON "line" corresponds to the content of the mysql row and the name of each JSON array is the name of the 'name' column.
The only thing I could manage for the moment is this :
$return_arr = array();
$sql = "SELECT * FROM bo_appart";
$result = mysql_query($sql) or die(mysql_error());
$index = 0;
while ($row = mysql_fetch_assoc($result)) {
$return_arr[$index] = $row;
$index++;
}
echo json_encode($return_arr);
And here is the output I get :
[
{"id":"11","name":"name1","k1":"foo","k2":"bar"},
{"id":"12","name":"name2","k1":"foo","k2":"bar"},
]
Thanks a lot !!!
UPDATED
Working code :
$return_arr = array();
$sql = "SELECT * FROM bo_appart";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$return_arr[ $row['nom_appart'] ][] = $row;
}
echo json_encode($return_arr);
}
You were close. I noticed you want final output to be an object not an array, because the outer brackets are {} not []. So you need a different object type, and you need to use each row's name as the key for storing that row.
$return_obj = new stdClass();
$sql = "SELECT * FROM bo_appart";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
$name = $row['name'];
$return_obj->$name = [$row]; // for PHP < 5.4 use array($row)
}
echo json_encode($return_obj);
This loop is enough, to create the desired JSON:
$return_arr = array();
while ($row = mysql_fetch_assoc($result)) {
$return_arr[$row['name']][] = $row; #or $return_arr[$row['name']] = [$row];
}
echo json_encode($return_arr);
I was working on it a lot of time and Create mash/mysql-json-serializer package.
https://github.com/AndreyMashukov/mysql-json-serializer
You can select Json_array of json_objects and etc. It support ManyToMany, oneToMany, manyToOne relations
SELECT JSON_ARRAYAGG(JSON_OBJECT('id',est_res.est_id,'name',est_res.est_name,'advert_groups',(SELECT JSON_ARRAYAGG(JSON_OBJECT('id',adg.adg_id,'name',adg.adg_name)) FROM advert_group adg INNER JOIN estate est_2 ON est_2.est_id = adg.adg_estate WHERE est_2.est_id = est_res.est_id))) FROM (SELECT * FROM estate est LIMIT 1 OFFSET 2) est_res
<?php
$sql = "SELECT * FROM bo_appart";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$return_arr[] = $row;
}
echo json_encode($return_arr);
?>

How can I retrieve data from a database using PHP?

I have a problem with retrieving data from a database using PHP statements. At the moment the output looks like:
[{"Bez":"Cino"},{"Bez":"Extra"},{"Bez":"Panini"},{"Bez":"And so on"}]
"Bez" is the label of the column and we don't want it to be displayed. We just want to display the cell's contents so it should look like:
[Cino,Extra,Panini,And so on]
The php is:
<?php
$username = "root";
$database = "kaffeehaus";
mysql_connect('localhost', $username);
#mysql_select_db($database) or die("Geht nicht!");
$query = "SELECT Bez FROM kategorie";
$result = mysql_query($query) or die(mysql_error("error"));
$num = mysql_numrows($result);
mysql_close();
$rows = array();
while ($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
echo json_encode($rows);
?>
Replace
$rows[] = $r;
With
$rows[] = $r['Bez'];
...though to be honest, I'd replace the whole "fetch" part with:
while($r = mysql_fetch_row($result))
$rows[] = $r[0];
That's because mysql_fetch_row is generally faster than mysql_fetch_assoc.
Replace
while ($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
}
with this:
while ($r = mysql_fetch_array ($result, MYSQL_ASSOC))
{
$rows[] = $r['Bez'];
}
Use mysql_fetch_row() instead (or mysql_fetch_array($result, MYSQL_NUM);). It fetches a numerical array so the column names are not shown.
Then, your output will look something like this:
[{"0":"Cino"},{"1":"Extra"},{"2":"Panini"},{"3":"And so on"}]
Remove the json_encode part:
echo json_encode($rows); ?>
And use bez as row key:
echo ($rows['bez']); ?>
And how does it work if there are more than one cell?
Our php looks like
<?php
$username = "root";
$database = "kaffeehaus";
mysql_connect('localhost', $username);
#mysql_select_db($database) or die("Geht nicht!");
$ID_Kategorie = $_GET["Kategorie"];
$query = "SELECT * FROM artikel WHERE ID_Kategorie=$ID_Kategorie";
$result=mysql_query($query) or die("error");
$num = mysql_numrows($result);
mysql_close();
$rows = array();
while ($r = mysql_fetch_assoc($result))
{
$rows = $r;
}
echo json_encode($rows); ?>
And the output like:
[{"ID_Artikel": "12","ID_Kategorie":"19","Bez":"blabla","Beschreibung":"blabla"}]
But it schould look like:
[{"12","19","blabla","blabla"}]

Categories