I am trying to push values from a sql query into an array using array_push.
Everything works fine except one field - "Beschreibung". It's a MySQL Text Field. When I put in the row "Beschreibung" the output from the PHP is completely empty.
Here is my code so far, any help is appreciated!
while ( $row = $result->fetch_assoc()) {
array_push($data, array(
"Beschreibung" => $row["Beschreibung"],
"id" => $row["id"],
"Titel_Veranstaltung" => $row["Titel_Veranstaltung"],
"Strasse" => $row['Strasse'],
"PLZ" => $row["PLZ"],
"Ort" => $row["Ort"],
"Bild" => $row["Bild"],
"Telefon" => $row["Telefon"],
"Datum" => $row["Datum"],
"Uhrzeit" => $row["Uhrzeit"],
"Latitude" => $row["latitude"],
"Longitude" => $row["longitude"],
"Teilnehmerzahl" => $row["Teilnehmerzahl"],
"Musikrichtung" => $row["Musikrichtung"],
"Art_Veranstaltung" => $row["Art_Veranstaltung"],
"Dauer_Veranstaltung" => $row["Dauer_Veranstaltung"]
));
}
echo json_encode($data);
Try this
$data = [];
while ( $row = $result->fetch_assoc()) {
$data[] = $row;
}
echo json_encode($data);
Related
Looking for some advice.
Trying to return a long query with around 65,000 results that has a lot of aggeration on the server-side. Takes around 16 seconds to return all the data.
This is what I have so far.
It's returning the query results but never seems to store in Memcached.
If I print $cached_data, it always says false. Thank you.
<?php
header('Content-Type: application/json');
include('pdo.php');
$memcache = new Memcache();
$memcache->addServer("127.0.0.1", 11211);
$query = "SELECT TOP 5000 Snumber,Number,Name,Status,dials,Size,Y04,Y05,PD0405,Y06,PD0506,Y07,PD0607,Y08,PD0708,Y09,PD0809,Y10,PD0910,Y11,PD1011,Y12,PD1112,Y13,PD1213,Y14,PD1314,Y15,PD1415,Y16,PD1516,Y17,PD1617,Y18,PD1718,Y19,PD1819,Y20,PD1920 FROM table ORDER BY Snumber";
$key = md5($query);
$cached_data = $memcache->get($key);
$response = [];
if ($cached_data != null) {
$result = $cached_data;
} else {
$statement = $conn->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$memcache->set($key, $result);
}
foreach ($result as $row) {
$output[] = array(
'Snumber' => $row['Snumber'],
'MeterNumber' => $row['Number'],
'Account' => $row['Name'],
'Status' => $row['Status'],
'dialsHI' => $row['dials'],
'MeterSize' => $row['Size'],
'Y04' => $row['Y04'],
'Y05' => $row['Y05'],
'PD0405' => $row['PD0405'],
'Y06' => $row['Y06'],
'PD0506' => $row['PD0506'],
'Y07' => $row['Y07'],
'PD0607' => $row['PD0607'],
'Y08' => $row['Y08'],
'PD0708' => $row['PD0708'],
'Y09' => $row['Y09'],
'PD0809' => $row['PD0809'],
'Y10' => $row['Y10'],
'PD0910' => $row['PD0910'],
'Y11' => $row['Y11'],
'PD1011' => $row['PD1011'],
'Y12' => $row['Y12'],
'PD1112' => $row['PD1112'],
'Y13' => $row['Y13'],
'PD1213' => $row['PD1213'],
'Y14' => $row['Y14'],
'PD1314' => $row['PD1314'],
'Y15' => $row['Y15'],
'PD1415' => $row['PD1415'],
'Y16' => $row['Y16'],
'PD1516' => $row['PD1516'],
'Y17' => $row['Y17'],
'PD1617' => $row['PD1617'],
'Y18' => $row['Y18'],
'PD1718' => $row['PD1718'],
'Y19' => $row['Y19'],
'PD1819' => $row['PD1819'],
'Y20' => $row['Y20'],
'PD1920' => $row['PD1920']);
}
echo json_encode($output, JSON_PRETTY_PRINT);
I have to populate a json with PHP.
I have this structure:
$request = array(
"api_uid" => "000000",
"api_key" => "xxxxxx",
"lista_articoli" => array(
//loop
array(
"nome" => "Acconto",
"descrizione" => "Acconto per la festa del " .$datafesta,
"prezzo_lordo" => $importo,
"cod_iva" => 0
)
// end loop
I try to use while inside array, but it's an error:
while($row = $tipologia->fetch(PDO::FETCH_ASSOC)) {
array("nome" => $row['nome'],
"descrizione" => $row['desc'],
"prezzo_lordo" => $row['prezzo_lordo'],
"cod_iva" => 0
),
}
How can i loop my data in correct way inside array?
You can't loop inside array.You need to create array and assign value to them by keys in loop.Try like below :
while ($row = $tipologia->fetch(PDO::FETCH_ASSOC)) {
$request['lista_articoli'][] = [
'nome' => $row['nome'],
'descrizione' => $row['desc'],
'prezzo_lordo'] => $row['prezzo_lordo'],
'cod_iva' => 0,
];
}
I did search for this but may be my search sucks. So posting it here..my code below
while( $row = mysqli_fetch_array($queryRecords) ) {
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
$marksheet = calculatepercent(totalmark),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
Here I call function calculatepercent(totalmark) so that the function returns a value and store that in $marksheet. But my problem is this does not work i.e I cannot store the result of calculatepercent(totalmark) because I cannot access 'totalmark'
Forget performance for a minute but how do you make this work? (If you have tips for performance that's a bonus too! ) - Thanks Coders!
RR
Try like this.Send $row['totalmark'] to the calculatepercent() function.
while( $row = mysqli_fetch_array($queryRecords) ) {
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
$marksheet => calculatepercent($row['totalmark']),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
I guess I was more desperate than any one else to get an answer! So after trying many things I got it working and sharing it here for any one else
while( $row = mysqli_fetch_array($queryRecords) ) {
$marksheet = calculatepercent($row['totalmark'];
$data['data'][] = array(
'student_name' => $row['name'],
'totalmark' => $row['totalmark'],
//move the fn 'calculatepercent' outside while loop $marksheet => //calculatepercent($row['totalmark']),
'resultdate' => $row['resultdate'].$marksheet,
'ID' => $row['ID']
);
I am trying to create an array of arrays in a loop to a JSON object, but It returns two objects instead. If I remove the array around the array with a key, it works but I need the key.
This is the format I am looking for:
$shop = array( "1408842145690" => array( id => "1408842145690",
code => "1",
title => "zdfdsf",
date => "2014-08-01",
description => "fghgf"
),
"1408840099517" => array( id => "1408840099517",
code => "1",
title => "test",
date => "2014-08-01",
description => "this is a test"
)
);echo json_encode($shop);
This is the code I am using
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array(
$row['task_id'] => array( 'id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
)
);
$alltasks[] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}
This is the result I get:
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01 00:00:00","description":"fghgf"}},{"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01 00:00:00","description":"this is a test"}}
This is the result I am looking for
{"1408842145690":{"id":"1408842145690","code":"1","title":"zdfdsf","date":"2014-08-01","description":"fghgf"},"1408840099517":{"id":"1408840099517","code":"1","title":"test","date":"2014-08-01","description":"this is a test"}}
Replace the contents of your while loop with the following:
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
EDIT: I tested the above, and it does work. Here is the full code with the replacement intact... try a copy/paste.
$query = " SELECT * FROM todolist ";
if ($result = mysqli_query($mysqli,$query)) {
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$tasks = array('id' => $row['task_id'],
'code' => $row['task_statusbox'],
'title' => $row['task_title'],
'date' => $row['task_date'],
'description' => $row['task_description']
);
$alltasks[$row['task_id']] = $tasks;
}
echo json_encode($alltasks);
/* free result set */
$result->close();
}
I am trying to creates the array dynamically like below using php
$data = array(
array("date" => "1/2/2012", "sentstatus" => "0", "mobile" => "14578998"),
array("date" => "21/2/2012", "sentstatus" => "1", "mobile" => "14668998"),
array("date" => "1/5/2012", "sentstatus" => "1", "mobile" => "14598998"),
array("date" => "1/6/2012", "sentstatus" => "0", "mobile" => "14578748"),
);
Below is my PHP code that insert the sql server data into array but the problem is that it the array is formed of only last result set row of the database table. I am not getting the idea to insert all database table row into array as shown above:
$sql = "SELECT [start_date_time],[sent_status],[mobile_number] ,[play_file]
FROM [slice].[dbo].[tbl_message_detail] ";
$res = odbc_exec($con,$sql) or die(odbc_error());
$rows = odbc_num_rows($res);
while($row = odbc_fetch_array($res))
{
$data = array(
array("Date_Time" => $row['start_date_time'], "Send_Status" => $row['sent_status'], "Mobile_Number" => $row['mobile_number'], "play_file" => $row['play_file'])
);
}
You are getting only the last row because you are creating a new array with every iteration. Declare $data outside of the while loop.
try this code:
while($row = odbc_fetch_array($res))
{
$data[] = array("Date_Time" => $row['start_date_time'],
"Send_Status" => $row['sent_status'],
"Mobile_Number" => $row['mobile_number'],
"play_file" => $row['play_file']);
}
You're overwriting the $data variable at each round of the loop. This:
$data = array();
while($row = odbc_fetch_array($res))
{
$data[] = array("Date_Time" => $row['start_date_time'],
"Send_Status" => $row['sent_status'],
"Mobile_Number" => $row['mobile_number'],
"play_file" => $row['play_file']
);
}
should work as you need