Home > Net >  creating json by data from mysql db and variable data
creating json by data from mysql db and variable data

Time:12-13

I have data from MySQL database and data from base64 file out of database I want to combine them so i can have one JSON data with both

$PaFile="JVBERi0xLjcKMS.....";
$NaFile="JVBERi0xLjvvm.....";
$sqlRun = mysqli_query($conn, "SELECT laimCode, laimYear, laimMonth FROM laim_folio");
        $row = mysqli_fetch_assoc($sqlRun);
        $json_array[] = $row;
        $jasondata = json_encode($json_array[]);

What i expect as output is

[{
 "laimCode":"1234",
 "laimYear":"2021",
 "laimMonth":"11",
 "PaFile":"JVBERi0xLjcKMS.....",
 "NaFile":"JVBERi0xLjvvm....."
}]

If i put these two variable in SQL as static column with value i can get the result i want But is there way to combine them outside SQL ? Like extends array with two extra field and then convert to JSON

CodePudding user response:

You can either add them to the array after fetching the data from the database and before adding into the overall array....

    $row = mysqli_fetch_assoc($sqlRun);
    $row['PaFile'] = "JVBERi0xLjcKMS.....";
    $row['NaFile'] = "JVBERi0xLjvvm.....";
    $json_array[] = $row;
    $jasondata = json_encode($json_array);

Or add the values to the SQL so they become part of the result set. So the value is just a literal and the alias becomes the column name...

SELECT laimCode, laimYear, laimMonth,
        "JVBERi0xLjcKMS....." as PaFile,
        "JVBERi0xLjvvm....." as NaFile
    FROM laim_folio
  • Related