Php 在搜索多个表的查询中检索表名

Php 在搜索多个表的查询中检索表名,php,mysql,database,multi-table,Php,Mysql,Database,Multi Table,如何修改以下查询代码以在结果中包含表名 $query搜索数据库中的4个表,我希望结果包括它们来自哪个表。所有4个表都有相同的字段 然后我想输出结果,如下面的$results\u数组[]数组所示 提前谢谢 $query = mysql_query("select * from $TableName_1 where Topic = '$topic' AND ((convert(`Post_Title` using utf8) like '%$term_1%') OR (convert(`Pos

如何修改以下查询代码以在结果中包含表名

$query搜索数据库中的4个表,我希望结果包括它们来自哪个表。所有4个表都有相同的字段

然后我想输出结果,如下面的$results\u数组[]数组所示

提前谢谢

$query = mysql_query("select * from $TableName_1 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_2 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_3 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_4 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
order by Post_Date desc LIMIT $items_to_query");

while($row = mysql_fetch_assoc($query)){
 $table_name = ?????????????????????;
 $db1 = $row['Post_Date']);
 $db2 = $row['Post_Title'];
 $db3 = $row['Author']);

 $results_array[] = '<div>'.$table_name.' - '.$db1.' - '.$db2.' - '.$db3.'</div>'."\n";
}
$query=mysql\u query(“从$TableName\u 1中选择*,其中Topic='$Topic'和
((使用utf8转换('Post_Title'),如“%$term_1%”)或
(转换(`Post_Title`使用utf8)如“%$term_2%”)或
(转换(`Post_Title`使用utf8)如“%$term_3%”)或
(转换(`Post_Title`使用utf8)如“%$term_4%”)
UNION select*从$TableName_2中选择,其中Topic='$Topic'和
((使用utf8转换('Post_Title'),如“%$term_1%”)或
(转换(`Post_Title`使用utf8)如“%$term_2%”)或
(转换(`Post_Title`使用utf8)如“%$term_3%”)或
(转换(`Post_Title`使用utf8)如“%$term_4%”)
UNION select*从$TableName_3中选择,其中Topic='$Topic'和
((使用utf8转换('Post_Title'),如“%$term_1%”)或
(转换(`Post_Title`使用utf8)如“%$term_2%”)或
(转换(`Post_Title`使用utf8)如“%$term_3%”)或
(转换(`Post_Title`使用utf8)如“%$term_4%”)
UNION select*从$TableName_4中选择,其中Topic='$Topic'和
((使用utf8转换('Post_Title'),如“%$term_1%”)或
(转换(`Post_Title`使用utf8)如“%$term_2%”)或
(转换(`Post_Title`使用utf8)如“%$term_3%”)或
(转换(`Post_Title`使用utf8)如“%$term_4%”)
按邮政日期描述限额$items(查询)订购;
while($row=mysql\u fetch\u assoc($query)){
$table_name=?????????????????????;
$db1=$row['Post_Date']);
$db2=$row['Post_Title'];
$db3=$row['Author']);
$results_array[]='.$table_name.'-'.$db1.-'.$db2.-'.$db3.'.“\n”;
}

谢谢,但是如何在$results_数组[]中获取表名呢?我只是用引号对其进行了编辑,以将$tableName变量作为字符串而不是表。使用$row['table_name'];我尝试了,但是没有检索到表名。我添加了$table=$row['table_name'];在while循环和$table中指向$results\u数组[],但我只得到了空白数据。还有其他帮助选项吗?
$query = mysql_query("select *, '$TableName_1' as table_name from $TableName_1 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_2' as table_name from $TableName_2 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_3' as table_name from $TableName_3 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_4' as table_name from $TableName_4 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
 (convert(`Post_Title` using utf8) like '%$term_2%') OR 
 (convert(`Post_Title` using utf8) like '%$term_3%') OR 
 (convert(`Post_Title` using utf8) like '%$term_4%')) 
order by Post_Date desc LIMIT $items_to_query");