3
$sql = 'SELECT COUNT(*) FROM ';
$cola = ' UNION '; // Deixar os espaços em branco "em volta".
$colaWhere = ' WHERE ';
$first = true;
$wheres = "";
foreach($arrayNomeBDs as $nomeBD){
$where = ' status = 2'. ' AND nomePessoa="O aqui vai variar" ';
if($first == true){
$wheres .= $sql . $nomeBD .$colaWhere . $where. $cola;
$first = false;
} else {
$wheres .= $sql . $nomeBD .$colaWhere . $where. $cola;
}
}
He is generating so:
SELECT COUNT(*) FROM tab1 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION SELECT COUNT(*) FROM tab2 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION SELECT COUNT(*) FROM tab3 WHERE status = 2 AND nomePessoa="O aqui vai variar" UNION
how do I remove the last Union? what implementation tip could you give me?
a good way is to check the last index of the array, if it is I do not put the UNION
? same thing if you only have a database name in the array, I should take the UNION