-2
I need to search with PHP for the data typed in two Mysql tables, produtos
and usuario
.
I’m doing like this:
$array = array();
$query = mysqli_query($conn,"SELECT * FROM produtos inner JOIN usuario ON produtos.nome = usuario.nome
WHERE produtos.nome LIKE '%{$query}%'");
while($row=mysqli_fetch_assoc($query)) {
$array[] = $row['nome'];
}
echo json_encode($array);
But nothing returns in the search, how could it solve this?
Bank structure:
produtos
id | nome
usuario
id_usuario | nome
I tried with the UNION
as I was instructed here but also did not give:
$query = mysqli_query($conn,"SELECT nome FROM produtos WHERE nome LIKE '%{$query}%' limit 5 UNION Select usuario FROM nome WHERE nome LIKE '%{$query}%' limit 5");
while($row=mysqli_fetch_assoc($query)) {
$array[] = $row['nome'];
}
echo json_encode($array);
What is the foreign key that makes the relation between the tables?
– Woss
foreign key in Mysql type Innodb
– Marcelo
Do you need to "search in two tables" or do you need to "search according to the relationship between them"? The two tables don’t seem to be related, but you still did a JOIN. JOIN serves to address the relationship between tables, not to unify and search.
– Woss
Where does the value of
$query
inLIKE '%{$query}%'
to define the variable$query
? This is wrong, it’s not!?– rbz
would have to be with UNION? how would it look?
– Marcelo
I tried with UNION but it didn’t work either
– Marcelo
But what is the result you expect? All products whose name is exactly the same as the user’s name? If so then use INNER JOIN, if not then explain what you want.
– anonimo
You have to have a field in the users/customers table that relates them to the product/merchandise table, for example a field
usuário.compra
whose value is aprodutos.id
– Augusto Vasques