0

I have 2 files where I retrieve data from a db and display as json format and I want to have only 1 file but I can't do it. This json will be imported in ionic 4. I tried to pyt both queries in a file, I got the data but if i want to import this data in ionic is not working in this mixed format. Any help please?

<?php 
header('Access-Control-Allow-Origin: *'); 
header('Access-Control-Allow-Methods: GET, POST ,OPTIONS, PUT'); 
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type,Accept");
 header('Content-Type: application/json'); $servername = "localhost"; 

$username = "admin"; $password = "marius"; $dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
  if (!$conn) 
   { 
    die("Connection failed: " . mysqli_connect_error());
   }

$sql = "SELECT id, cname, custom, alch, description FROM drinks";
$result = mysqli_query($conn, $sql); 

$json_array = array();
 while ($row = mysqli_fetch_assoc($result)) 
   {
   $json_array[] = $row;
   }
echo json_encode($json_array); 

mysqli_close($conn); 
?>

and this is the second one:

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST ,OPTIONS, PUT');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header('Content-Type: application/json');

$servername = "localhost";
$username = "admin";
$password = "marius";
$dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
 die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, pname, img_dir FROM images";


$result = mysqli_query($conn, $sql);


$json_array = array();

while ($row = mysqli_fetch_assoc($result))
{
 $json_array[] = $row;
}

echo json_encode($json_array);

mysqli_close($conn);
?>

Both retrieving data from same table and same db

I tried with multi-queries but I can't figure out how to print data as json

0 Answers0