I'm trying to display data to bootstrap datatable (4 columns) using php and ajax from mysqli_multi_query().
I could not get the 2nd query working with UNION or INNER JOIN.
I'm no programmer and managed to, almost, get the correct result, but for some reason the 1st query returns "null" value.
How can I change that?
Here is a log sample and my code:
PHP Notice: Undefined offset: 2 on line 26 PHP Notice: Undefined offset: 3 on line 27 {"sEcho":1,"iTotalRecords":1,"iTotalDisplayRecords":1,"aaData":{"CommonInverterData":[{"Date":null,"Generated":null,"Export":"0.9921563111569116","Import":"1.8864974578334937"}
/* Database connection start */ 
include ('db.php');
$conn = mysqli_connect($hn, $un, $pw, $db) or die("Connection failed: " . mysqli_connect_error());
/* Database connection end */
$sql = "SELECT m.`date`, `day_energy`
        FROM `CommonInverterData`
        JOIN ( SELECT `date`, MAX(`time`) 'maxtime'
        FROM `CommonInverterData`
        GROUP BY `date`) m
        ON m.maxtime = `CommonInverterData`.`time`
        AND m.`date` = `CommonInverterData`.`date`;";
$sql .= "SELECT ABS(SUM((CASE WHEN `P_Grid`<0 THEN `P_Grid` ELSE 0 END) / 60000 )) as 'Export', SUM((CASE WHEN `P_Grid`>=0 THEN `P_Grid` ELSE 0 END) / 60000 ) as 'Import' FROM `PowerFlowRealtimeData` GROUP BY `date`;";
if (mysqli_multi_query($conn, $sql) or die(mysqli_error($conn))) {
    do {
        if ($result=mysqli_store_result($conn)) {
            $data = array();
            while( $rows = mysqli_fetch_row($result) ) {
                $data[] = array(
                    'Date' => $rows[2],
                    'Generated' => $rows[3],
                    'Export' => $rows[0],
                    'Import' => $rows[1],
                );
            } mysqli_free_result($result);
        }
    } while (mysqli_next_result($conn));
}
mysqli_close($conn);
$return = array(
    "sEcho" => 1,
    "iTotalRecords" => count($data),
    "iTotalDisplayRecords" => count($data),
    "aaData"=>$data);
echo json_encode($return);
 
     
    