Te paso el codigo completo para que le des un vistazo:
Código PHP:
extract($_GET);
if(is_array($tablas))
{
$arreglo = "";
// Obtiene las tablas a las cuales se les hará la consulta
foreach($tablas as $valor)
{
$arreglo = $arreglo . $valor . ",";
$total_tbl++;
}
//Declara variables con el criterio a buscar
$cons = "";
$cons2 = "";
if(($_GET['apaterno']!=="")) { $cons = $cons . $_GET['apaterno'] . " "; }
if(($_GET['amaterno']!=="")) { $cons = $cons . $_GET['amaterno'] . " "; }
if(($_GET['nombres']!=="")) { $cons = $cons . $_GET['nombres']; }
if(($_GET['nombramiento']!=="")) { $cons2 = $cons2 . $_GET['nombramiento']; }
// Obtiene todas las tablas de la base de datos
$sql_tot = "SHOW TABLES FROM pgje";
$result = mysql_query($sql_tot);
while ($row = mysql_fetch_array($result))
{
// Esto es para comparar las seleccionadas con el foreach vs while
if (strpos($arreglo,$row[0]) !== false)
{
/*echo "<script>alert('arreglo=".$arreglo." /tablas=".$row[0]."');</script>";*/
$suma++;
echo "<script>alert('suma=".$suma." /tablas=".$total_tbl."');</script>";
if($suma < $total_tbl)
{
// REALIZA SQL CON UNION
$sql[] = "SELECT `".$row[0]."`.`CONS`,`".$row[0]."`.`EMP_NOM`,`".$row[0]."`.`NOMBRAMIENTO` FROM `".$row[0]."` WHERE `".$row[0]."`.`EMP_NOM` LIKE '%".$_GET['apaterno']."%' UNION ";
echo "<br><br>";
}
else
{
// REALIZA SQL SIN UNION
$sql[] = "SELECT `".$row[0]."`.`CONS`,`".$row[0]."`.`EMP_NOM`,`".$row[0]."`.`NOMBRAMIENTO` FROM `".$row[0]."` WHERE `".$row[0]."`.`EMP_NOM` LIKE '%".$_GET['apaterno']."%'";
echo "<br><br>";
/* FOREACH ES PARA SACAR EL VALOR DE $sql[] Y PASARLO A LA VARIABLE $valr */
foreach($sql as $valr)
{
$alo = $alo . $valr;
}
// $alo es el string que tiene toda la consulta $sql[]
echo "IMPRIMIR ALO:<BR><BR>";
echo $alo;
$algo = mysql_query($alo,$conexion);
while($res_sql = mysql_fetch_array($algo))
{
echo "<table width='600'><TR><TD>".$res_sql2["CONS"]."</td></tr>";
echo "<TR><TD>".$res_sql2["NOMBRAMIENTO"]."</td></tr>";
echo "<TR><TD>".$res_sql2["EMP_NOM"]."</td></tr></table>";
}
}
}
}
}
No es asi que digamos un super codigasso pero si me hace lo que necesito jeje.
Saludos