1
Hello, everybody all right?
Does anyone know how to get the name of the respective table of the pulled value?
I have the following code:
$query= "
SELECT DISTINCT titulo, categoria, views
FROM (
SELECT DISTINCT titulo, categoria, views FROM secao_aves UNION ALL
SELECT DISTINCT titulo, categoria, views FROM secao_peixes
) as dados
ORDER BY views DESC
LIMIT 3
";
Then I take the data and I spin the loop:
//PEGA OS DADOS
if($stmt_mvw = $mysqli->prepare($query)){ /* INICIA DECLARAÇÃO PRINCIPAL */
//ABRE DECLARAÇÃO
$stmt_mvw->execute();
//TRAZ O RESULTADO DA CONSULTA
$stmt_mvw->bind_result($titulo, $categoria, $views);
while($stmt_mvw->fetch()){
echo '<a href="busca.php?table='.$NOME_DA_TABELA.'">'.$titulo.'('.$views.' visualizações)</a>';
}
}
Let’s say my database is like this:
Table "secao_aves":
id | titulo | categoria | views |
1 | Pardal | AA | 250 |
2 | Beija-Flor | AB | 100 |
3 | João-de-Barro | AC | 145 |
Table "secao_pisces":
id | titulo | categoria | views |
1 | Bagre | PX | 180 |
2 | Dourado | PY | 165 |
3 | Pintado | PZ | 75 |
So far, quiet. For example, it would be printed this (the 3 records with more "views"):
<a href="busca.php?table=<?>">Pardal (250 visualizações)</a>
<a href="busca.php?table=<?>">Bagre (180 visualizações)</a>
<a href="busca.php?table=<?>">Dourado (165 visualizações)</a>
But actually, I’d like you to print it out like this:
<a href="busca.php?table=secao_aves">Pardal (250 visualizações)</a>
<a href="busca.php?table=secao_peixes">Bagre (180 visualizações)</a>
<a href="busca.php?table=secao_peixes">Dourado (165 visualizações)</a>
In this case, I don’t know how to pull the table name relative to the data, to create the variable $NOME_DA_TABELA :/
How to proceed? v
Try to put in internal consultations something like
select título, categoria, views, 'nome da tabela' as tabela
, so each record will have the table name as a column.– Woss
Oops, thank you, Anderson. I went to see the answers now. Thanks again for being willing, it really was something simple.
– Alex Lupóz