your query must be in UNION
:
Select men.Name, men.age, men.city, men.country, 'Male' as 'gender' FROM men_table as men UNION ALL Select women.Name, women.age, women.city, women.country, 'Female' as 'gender FROM women_table as women;
so you can have an query output:
|Name |age |city |country|gender|
----------------------------------------
|male1 |21 |NY |USA |Male |
|male2 |23 |CLV |USA |Male |
|female1|25 |GS |USA |Female|
|female2|27 |CHG |USA |Female|
after that you can loop to your result:
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s
", $mysqli->connect_error);
exit();
}
$query = "Select men.Name, men.age, men.city, men.country, 'Male' as 'gender' FROM men_table as men UNION ALL Select women.Name, women.age, women.city, women.country, 'Female' as 'gender FROM women_table as women";
$men = array();
$women = array;
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
if($row["gender"] == 'Male') {
$men[] = $row[];
}
else {
$women[] = $row[];
}
}
print_r($men);
print_r($women);
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
The output will be like:
//$men array
array(
[0] => array(
'Name' => 'male1',
'age' => '21',
'city' => 'NY',
'country' => 'USA',
'gender' => 'Male'
),
[1] => array(
'Name' => 'male2',
'age' => '23',
'city' => 'CLV',
'country' => 'USA',
'gender' => 'Male'
)
);
//$women array
array(
[0] => array(
'Name' => 'female1',
'age' => '25',
'city' => 'GS',
'country' => 'USA',
'gender' => 'Female'
),
[1] => array(
'Name' => 'female2',
'age' => '27',
'city' => 'CHG',
'country' => 'USA',
'gender' => 'Female'
)
);