result: "No Data Found"

I try to display the data of two tables at the same time but that did not work for me.

<?php
require_once 'cont.php';
$resultAll = mysqli_query($con, "SELECT * FROM topic  UNION  SELECT * FROM Image ");
if ($resultAll) {
     while($row[] = $resultAll->fetch_assoc()) {     
     $item = $row;   
     $json = json_encode($item, JSON_NUMERIC_CHECK);     
     } 
} else {
    $json = json_encode(["result" => "No Data Found"]);
}
 echo $json;
$con->close();
?>

I get a message:

result" => "No Data Found

How can show two queries at one time? What error do I have in my code?

>Solution :

In order to use UNION statement below are general rules:

  • Every SELECT statement within UNION must have the same number of columns
  • The columns must also have similar data types
  • The columns in every SELECT statement must also be in the same order

As you are using SELECT * FROM table in both your queries, I guess the number of columns aren’t same. Try getting only necessary columns.

Leave a Reply