如何知道fetch_array中的表名

I am doing a query with union from two different tables and then on the fetch_array loop I would like to know from which table out of the two I am actually grabbing, anyway without adding a flag into the table's structures. ?

The flag doesn't need to be in the table, but can easily be in the query:

SELECT 'table1' as t, ... FROM table1
UNION
SELECT 'table2' as t, ... FROM table2

...

echo $row['t'];

You don't have to select fields from a table, you can simply "select a string literal" too.

If you have columns with identical name in both tables you could use as

SELECT table1.col1 as col1, table1.col2, table1.col3 FROM table1
UNION
SELECT table2.col1 as col4, table1.col5 FROM table2

then when you do $data = fetch_assoc($q) you will have

$data["col1"] // table1.col1
$data["col2"] // table1.col2
-----------------------------
$data["col4"] //table2.col1