Search code examples
phpmysqljoinstore

Php Join two tables


I have two tables

Table 1 category

Id    name      img   
1   appliances   
2   books

Table 2 products

ID  name des  price  cataegory sub  img
1  TV    lcd  125     1        tv

I am trying to join the two tables that in category in table 2 ill get the name from table 1

That it will show as

ID  name des  price  cataegory sub  img
1  TV    lcd  125  appliances    tv  

this is my code

    $sql = "SELECT p.id, 
                   p.name,
                   p.description, 
                   p.price,
                   c.name,
                   p.sub
            FROM products p
            JOIN category c on products p = c.id
            ORDER by p.id
            ";

    if($query =  mysql_query($sql) or die())
    {
        while($result = mysql_fetch_assoc($query))
        {   echo "<tr id='id'>";
            //echo "<td>".$result['img']."</td>";
            echo "<td>".$result['name']."</td>";
            echo "<td>".$result['description']."</td>";
            echo "<td>".$result['price']."</td>";
            echo "<td>".$result['category']."</td>";
            echo "<td>".$result['sub']."</td>";
            echo "</tr>";
        }
    }       ";

Solution

  • FROM products p
            JOIN category c on products p = c.id
    

    It should be ON p.id = c.id or what the matching param is i.e. p.cataegory = c.id