SQL INNER JOIN Syntax Tutorial PHP MySQL Database
Learn SQL INNER JOIN syntax for accessing data from two related tables in a single query according to clauses on the data. It will render a result set that has all selected columns from both tables.
<?php
// Connect to your database
$db_conx = mysqli_connect("localhost", "db_user", "db_password", "db_name");
if (!$db_conx) { die( mysqli_connect_error() ); }
// Query and build the display list
$list = "";
$sql = "SELECT people.username, people.country, options.youtube
FROM people INNER JOIN options
ON people.id = options.id
WHERE options.youtube != '' ORDER BY people.id ASC";
// the ON clause specifies that the "id" column from both tables must match
$query = mysqli_query($db_conx, $sql) or die( mysqli_error($db_conx) );
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$list .= $row["username"]." from ".$row["country"].' has a Youtube Channel: ';
$list .= '<u>https://www.youtube.com/user/'.$row["youtube"].'</u> <hr>';
}
// Close the database connection
mysqli_close($db_conx);
echo $list;
?>
SELECT people.username, people.country, options.youtube
FROM people INNER JOIN options
ON people.id = options.id
WHERE options.youtube != '' ORDER BY people.id ASC