1

I have a database which holds the tables:

fish (values are name, image, information, cooking_type_id) and cooking_type (values are cooking_type_id, thumbnail)

I've created a very simple mysqli query to display the data from the first table, but I want to display the thumbnail according to the id.

So for cooking_type maybe "Grill" has a cooking_type_id of 1. The fish "Cod" is suitable for grilling therefor it holds the value 1 under "type_id".

Instead of the displaying the "1" how do I display the thumbnail?

Here's my query code:

$query = "SELECT * FROM `fish`";
    $result = $mysqli->query($query);
    $mysqli->set_charset("utf8");

    while($row = mysqli_fetch_array($result)){  
        echo '<div class="row" style="background-color:#fff;">';

        echo '<div class="col-sm-2">';
        echo '<div class="listphoto">';
        echo $row['image'];
        echo '</div></div>';

        echo '<div class="col-sm-2">';
        echo '<div class="listtext"><h3>';
        echo $row['name'];
        echo '</h3></div></div>';

        echo '<div class="col-sm-2">';
        echo '<div class="typephoto">';
        echo $row['type_id'];
        echo '</div></div>';
        echo '</div>';
    }

    mysqli_close($conn);
1
  • 1
    You can use inner join on cooking_type_id.
    – Codelord
    May 13, 2015 at 12:23

4 Answers 4

1
$query = "SELECT fish.image, fish.name, cooking_type.thumbnail FROM fish
          JOIN cooking_type
          ON fish.cooking_type_id = cooking_type.cooking_type_id;";
    $result = $mysqli->query($query);
    $mysqli->set_charset("utf8");

    while($row = mysqli_fetch_array($result)){  
        echo '<div class="row" style="background-color:#fff;">';

        echo '<div class="col-sm-2">';
        echo '<div class="listphoto">';
        echo $row['image'];
        echo '</div></div>';

        echo '<div class="col-sm-2">';
        echo '<div class="listtext"><h3>';
        echo $row['name'];
        echo '</h3></div></div>';

        echo '<div class="col-sm-2">';
        echo '<div class="typephoto">';
        echo $row['thumbnail'];
        echo '</div></div>';
        echo '</div>';
    }

    mysqli_close($conn);
3
  • 1
    Are you sure that array keys will include table prefixes? I think that it should be $row['image'] not $row['fish.image'] and so on, but I base it on pdo_pgsql.
    – Furgas
    May 13, 2015 at 12:51
  • The problem I have here is that name is assigned to both fish and cooking_type. But I solved this by changing the names to f.name and ctype.name.
    – MstrQKN
    May 13, 2015 at 18:48
  • 1
    @MstrQKN You can use column aliases ex. select fish.name as fish_name, cooking_type.name as cooking_type_name.
    – Furgas
    May 14, 2015 at 6:32
0

Use the JOIN SQL feature :

SELECT * 
FROM `fish` 
INNER JOIN `cooking_type` 
    ON `cooking_type`.`cooking_type_id` = `fish`.`cooking_type_id`

Doc: MySQL JOIN SYNTAX

0

try this

select f.*, ct.* from `fish` as f
left join `cooking_type` as ct
on f.cooking_type_id = ct.cooking_type_id
0

Using "INNER JOIN" would solve your problem easily :

 Select * from fish 

 inner join 'cooking_type' on ............etc, etc

MYSQL INNER JOIN

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.