SQL JOIN tables example (INNER JOIN), PHP

We have two tables “cats” and “dogs”.

1
2
3
4
5
6
7
8
<?php
$id = 1;
$requestSQL = mysqli_query($connection, "SELECT * FROM cats INNER JOIN dogs on (id = id_dog) where id = '$id'");
while ($row = mysqli_fetch_array($requestSQL))
{
echo ''.$row['name'];
}
?>

So I will explain.

The query will only trigger the results from the dogs table as the INNER JOIN will activate the second table, in our case “dogs”. They will get activated because the id and id_dog will match. Both tables will have the value set to “1”.

The $row[‘name’]; could just display the name of a dog that has the id_dog as 1;

Whatever command you will pull from your query will be from the dogs table. Count, delete or whatever.

Coding

ADD A COMMENT: