I am new to php and am trying to return a json response in a particular structure. Here is what I have tried so far:
$response = array();
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branchId'] = $row['branchId'];
$response[$x]['branch'] = $row['branch'];
$response[$x]['customerGroupId'] = $row['customerGroupId'];
$response[$x]['customerGroup'] = $row['customerGroup'];
$response[$x]['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}
The code above returns this response:
However, instead of returning for example "branchId" and "branch" as individual properties, I want to pass their values inside a branchObject such that branch.id == "branchId" and branch.name == "branch".I mean, How may I return the response in the following structure:
And Here is how my database looks like:
How can I achieve this?
You ask for stuff that we are unsure if db result returns but as nice_dev pointed out, you need something like this:
$response = [];
if ($con) {
$sql = "select * from admission_view";
$result = mysqli_query($con, $sql);
if ($result) {
$x = 0;
while ($row = mysqli_fetch_assoc($result)) {
$response[$x]['id'] = $row['id'];
$response[$x]['name'] = $row['name'];
$response[$x]['isActive'] = $row['isActive'];
$response[$x]['branch']['id'] = $row['branchId'];
$response[$x]['branch']['name'] = $row['branch'];
$response[$x]['customerGroup']['id'] = $row['customerGroupId'];
$response[$x]['customerGroup']['name'] = $row['customerGroup'];
$response[$x]['customerGroup']['orderNo'] = $row['orderNo'];
$x++;
}
echo json_encode($response, JSON_PRETTY_PRINT);
}
} else {
echo "Connection error";
}
Related
I have created API from my DB and the result appears in a PHP file, so the question is how I can read this result in a .json file?
<?php
$con = mysqli_connect("localhost","root","Password##","Mydb");
$response = array();
if($con){
$sql = "select * from limit_order";
$result = mysqli_query($con,$sql);
if($result){
header("Content-Type: JSON");
$i=0;
while($row = mysqli_fetch_assoc($result)){
$response[$i]['order_type'] = $row ['order_type'];
$response[$i]['from'] = $row ['from'];
$response[$i]['to'] = $row ['to'];
$response[$i]['quantity'] = $row ['quantity'];
$response[$i]['limit_price'] = $row ['limit_price'];
$response[$i]['created_at'] = $row ['created_at'];
$response[$i]['updated_at'] = $row ['updated_at'];
$response[$i]['status'] = $row ['status'];
$i++;
}
echo json_encode($response,JSON_PRETTY_PRINT);
}
}
else{
echo "Database connection failed";
}
?>
json return this, images are equals
How to return image data from MySQL? This code returns always first image.When I fetching only one value, everything is ok.
Please help.
require_once 'connection.php';
mysqli_query ($conn,"SET NAMES UTF8");
$sql = "SELECT * FROM posts WHERE authorId = '$id' GROUP BY date DESC ";
$response = mysqli_query($conn, $sql);
$result['posts'] = array();
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index['id'] = $row['id'];
$index['authorId'] = $row['authorId'];
$index['date'] = $row['date'];
$index['time'] = $row['time'];
$index['description'] = strval($row['description']);
$index['picture'] = base64_encode($row['picture']);
#echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['picture'] ).'"/>';
array_push($result['posts'], $index);
$index['picture'] = null;
}
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
} else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result, JSON_UNESCAPED_UNICODE);
mysqli_close($conn);
}
Set $index to an empty array at the start of the while loop, this should clear any existing values from the previous iteration
you are not resetting $index variable on each row.
just add $index=array(); as below.
if ( mysqli_num_rows($response) > 0 ) {
while ($row = mysqli_fetch_array($response)){
$index = array(); // this one.
}
}
Firstly I got the workers name from BIRTHDAYS and then want to get e-mail address from USERS.There is no problem to take workers name's from Table1 but when I try to get the e-mail addresses the db returns me NULL.My DB is mssql.
<?php
include_once("connect.php");
$today = '05.07';
$today1 = $today . "%";
$sql = "SELECT NAME FROM BIRTHDAYS WHERE BIRTH LIKE '$today1' ";
$stmt = sqlsrv_query($conn,$sql);
if($stmt == false){
echo "failed";
}else{
$dizi = array();
while($rows = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$dizi[] = array('NAME' =>$rows['NAME']);
$newarray = json_encode($dizi,JSON_UNESCAPED_UNICODE);
}
}
foreach(json_decode($newarray) as $nameObj)
{
$nameArr = (array) $nameObj;
$names = reset($nameArr);
mb_convert_case($names, MB_CASE_UPPER, 'UTF-8');
echo $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn,$sql2);
if($stmt2 == false)
{
echo "failed";
}
else
{
$dizi2 = array();
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
}
}
?>
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
you put in $rows1 and would take it from $rows NULL is correct answer :)
take $rows1['EMAIL'] and it would work
and why foreach =?
you can put the statement in while-loop like this:
while ($rows = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$names = $rows['NAME'];
$sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn, $sql2);
if ($stmt2 == false) {
echo "failed";
} else {
$dizi2 = array();
while ($rows1 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$dizi1[] = array('EMAIL' => $rows1['EMAIL']);
echo $newarray1 = json_encode($dizi1, JSON_UNESCAPED_UNICODE);
}
}
}
I'm running PHP and MySQL and have the following code:
$data = array();
$result = mysql_query($search_query);
if ($result){
while($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
If my query has no rows I do get empty returned.
But if there's any records I get a Resource has no content in Safari.
But if I uncomment my //var_dump($data); then I do get a nice array of values.
Try this:
// Database connection.
$mysqli = new mysqli('localhost', 'user', 'password', 'db_name');
// Your query.
$search_query = "SELECT * FROM yuor_table";
$data = array();
$result = $mysqli->query($search_query);
if ($result){
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
if (sizeof($data) > 0) {
//var_dump($data);
echo json_encode($data);
} else {
echo 'empty';
}
}
This is very simple solution. I would suggest to use "mysqli".
this is my code :
<?php
$response = array();
include("konek.php");
$result = "SELECT NAMA_RS, ALAMAT, LATITUDE, LONGITUDE FROM RUMAH_SAKIT";
$statement = oci_parse($c, $result);
oci_execute($statement, OCI_DEFAULT);
if (oci_num_rows($statement) > 0) {
$response["daftar_rs"] = array();
while ($row = oci_fetch_array($statement)) {
$daftar_rs = array();
$daftar_rs["nama_rs"] = stripslashes($row["NAMA_RS"]);
$daftar_rs["alamat_rs"] = stripslashes($row["ALAMAT"]);
$daftar_rs["latitude_rs"] = stripslashes($row["LATITUDE"]);
$daftar_rs["longitude_rs"] = stripslashes($row["LONGITUDE"]);
array_push($response["daftar_rs"], $daftar_rs);
}
}
$response["success"] = 1;
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "error";
echo json_encode($response);
}
?>
but json is not running properly,
display in php empty.
what should I do?
You never add the data to the response variable.
$response["daftar_rs"] = array();
while ($row = oci_fetch_array($statement)) {
$daftar_rs = array();
$daftar_rs["nama_rs"] = stripslashes($row["NAMA_RS"]);
$daftar_rs["alamat_rs"] = stripslashes($row["ALAMAT"]);
$daftar_rs["latitude_rs"] = stripslashes($row["LATITUDE"]);
$daftar_rs["longitude_rs"] = stripslashes($row["LONGITUDE"]);
$response["daftar_rs"][] = $daftar_rs;
}