1

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
M Al
  • 357
  • 7
  • 15
  • 1
    Your error message is misleading because queries don't fail when no rows match. If you are using an old PHP version you need to check for MySQL error messages manually, PHP won't report them automatically: https://phpdelusions.net/mysqli/error_reporting#tldr – Álvaro González Sep 27 '22 at 12:09
  • Hi bro / I use PHP Version 7.4.30 @ÁlvaroGonzález – M Al Sep 27 '22 at 12:11
  • 2
    `UNION` links two queries that have the same fields (with the same name!), so that one is concatenated to the other. If `topic` and `Image` are tables with different number of name of fields, the query returns an error and thus it evaluates to `false`. How would you like to show your queries? What did you expect? – Enrico Agrusti Sep 27 '22 at 12:11
  • 1
    Please use error reporting and provide the error(s). – user3783243 Sep 27 '22 at 12:13
  • 1
    If `topic` and `Image` have some related field you should use a `join`, not a `union`. Questions doesn't have enough info to answer yet. – user3783243 Sep 27 '22 at 12:15
  • @user3783243 Hello brother, I apologize for that. I misunderstood the work. I need to make two queries at the same time, but the fields are different, how can this be done? – M Al Sep 27 '22 at 12:18
  • @user3783243 Hi bro / I don't want use join because I will select multi image from table image when I use join just I get one image – M Al Sep 27 '22 at 12:20
  • @MAl Please provide example structure and what you want to get back. – user3783243 Sep 27 '22 at 12:21
  • 1
    If you need to run two entirely separate queries, then simply use two mysqli_query commands. You can always use php afterwards to combine the results if necessary, but I don't know - you haven't actually shown us any sample data from your tables or told us what exact output you want at the end. Therefore it's hard to be sure what the correct query (or queries) should really be – ADyson Sep 27 '22 at 12:26
  • @user3783243 Hi bro can you please see this : https://stackoverflow.com/questions/73868228/how-to-inner-join-two-tables-to-get-same-colum-name-php-mysql – M Al Sep 27 '22 at 13:16
  • @ADyson Hi bro can you please see this : https://stackoverflow.com/questions/73868228/how-to-inner-join-two-tables-to-get-same-colum-name-php-mysql – M Al Sep 27 '22 at 13:16
  • 2
    @MAl Why is there an answer selected here if it didn't resolve issue? – user3783243 Sep 27 '22 at 13:21

1 Answers1

1

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.

Zankrut Parmar
  • 1,872
  • 1
  • 13
  • 28