Pregunta

How can I modify the following query code to include the table names in the results?

The $query searches 4 tables in the database and I want the results to include which table they came from. All 4 tables have identical fields.

I then want to output the results as shown in the $results_array[] array below.

Thanks in advance.

$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";
}
¿Fue útil?

Solución

$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");
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top