简体   繁体   中英

Selecting multiple tables PHP

I'm having issues with this code, it's displaying multiple of the same table headings and I don't know why.

Heres the code:

#This section of code will display my affected_countries table
echo"<br><br>This will list and display important information about the statistics of the infections<br>";
$sql = "SELECT * from affected_countries,infection_info";

$result = mysqli_query($db, $sql);

echo"<table border='1'><tbody>";
echo"<tr><td><b>Name</b></td><td><b>Country Origin</b></td><td><b>Number of Countries affected</b></td></tr>";

while($row = mysqli_fetch_assoc($result)) {
    echo"<tr><td>{$row['NAME']}</td><td>{$row['Country Origin']}</td><td>{$row['Number of Countries affected']}</td></tr>";
}
echo"</tbody></table><br>";

Output on website: Two Tables above Table below(Issue) It just has multiple of the same thing and I don't know what I should do

Any help is appreciated

You have to use SQL joins to get columns from multiple tables. See the example query:

SELECT ac.name, ii.country_origin, ii.number_of_countries_affected FROM affected_countries ac INNER JOIN infection_info ON ac.name =ii.name;

You should list from two tables separately

$sqlAffected = "SELECT * from affected_countries";
$sqlinfection_info = "SELECT * from infection_info";

$resultA = mysqli_query($db, $sqlAffected);
$resultB = mysqli_query($db, $sqlinfection_info);

or something like this:

SELECT * FROM affected_countries UNION infection_info;

or

SELECT * FROM affected_countries LEFT JOIN infection_info on afftected_countries.primary_key = infection_info.affected_country_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM