List all data with the same id

0

I have the following problem: I have a table named pedido . In this table I get data from two other tables:

I'dliketomakealistofonlyproductsthathavethesameid_venda.Inmycodeonlythefirstitemaddedwithequalidappears:

<?php$perfil1=mysql_query("SELECT * FROM pedido WHERE id_venda='$id'");

$lista=list($id, $produtosb , $idvenda)=mysql_fetch_row($perfil1);


$prod = "SELECT * FROM produtos WHERE id_produto=". $lista[1];
$query = mysql_query($prod);
$b=mysql_fetch_array($query);
$prod = $b ['produtos'];

?>

<input type="text" name="id" style="width: 450px" readonly="true"  value="<?php echo $prod; ?>"><br>

I tried to use the while loop, but it returns all the data in the table. What repetition loop should I use?

    
asked by anonymous 28.12.2015 / 20:38

1 answer

3

Come on ...

Based on the principle that the produtos column is the sales ID of the sale in the pedidos table, and with id_venda set, we solve the problem with a query only:

Assuming your produtos column has:

id | nome

Here's the select:

$SQL = "SELECT * FROM produtos WHERE id_produto in (SELECT produtos FROM pedido WHERE id_venda='$id')";
$result = mysql_query($SQL);

while ($db_field = mysql_fetch_assoc($result) ) {

print $db_field['ID'] . "<BR>";
print $db_field['nome'] . "<BR>";

}

EDIT:

Use with join:

$SQL = "SELECT * FROM pedido inner join produtos on produtos = id_produto where id_venda='$id'";
    
28.12.2015 / 20:46