Here is example of a database. We call this database table Player
:
+---------+----------+-------------+------------+------------+--+
| Name | pid | cash | bankacc | Random | |
+---------+----------+-------------+------------+------------+--+
| carl | 123 | non | 3434343434 | 34343433 | |
| petter | 456 | non | 3434343434 | 3434343434 | |
| sam | 1337 | non | 3434343434 | 3434343434 | |
| | | non | 3434343434 | 3434343434 | |
+---------+----------+-------------+------------+------------+--+
Here is another table called House
; in house is only display the PID
:
+---------+----------+-------------+------------+------------+--+
| pid | owned | pos | random | Random | |
+---------+----------+-------------+------------+------------+--+
| 123 | categ | non | 3434343434 | 34343433 | |
| 456 | categ | non | 3434343434 | 3434343434 | |
| 1337 | tag | non | 3434343434 | 3434343434 | |
| 4 | tag | non | 3434343434 | 3434343434 | |
+---------+----------+-------------+------------+------------+--+
I'm trying to display this table called "House" in my php site, but I do not want to display the "pid" instead of "pid" I want the name of the house owner.
$sql = "SELECT pid, pos, owned FROM houses";
$ru = $conn->query($sql);
while ($row = $ru->fetch_assoc()) {
echo '<tr>';
echo '<td>'.$row['pid'].'</td>'; <--instead of pid I want the name in here
echo '<td>'.$row['pos'].'</td>';
echo '<td>'.$row['owned'].'</td>';
echo '<td class="text-right">';
echo '<button class="button tiny">View User</button>';
echo '<button class="button alert tiny">Delete</button>';
echo '</td>';
echo '</tr>';
}
?>
But here is example of a query how I want it. Really bad example.
$sql = "SELECT pid = name, pos, owned FROM houses, player";
use join to get the name from player table
SELECT player.name, house.pos, house.owned
FROM houses house
LEFT JOIN player player on player.pid=house.pid
code
$sql = "SELECT player.`name` as owner_name, hs.pos, hs.owned
FROM houses hs
LEFT JOIN player pl on pl.pid=hs.pid";
$ru = $conn->query($sql);
while ($row = $ru->fetch_assoc()) {
echo '<tr>';
echo '<td>'.$row['owner_name'].'</td>'; <--instead of pid i want the name in here
echo '<td>'.$row['pos'].'</td>';
echo '<td>'.$row['owned'].'</td>';
echo '<td class="text-right">';
echo '<button class="button tiny">View User</button>';
echo '<button class="button alert tiny">Delete</button>';
echo '</td>';
echo '</tr>';
}
MySQL fetch Row Tutorial: https://www.w3schools.com/php/func_mysqli_fetch_row.asp
some notes:
better to change the column 'name' to 'owner_name' to avoid any conflicts or use backticks(`) around column names when you use reserved keywords in query:
Thank you guys i got it work :)
This is the working code how i changed it a bit
$sql = "SELECT players.name as owner_name, houses.pos, houses.owned FROM houses LEFT JOIN players ON players.pid=houses.pid";
$ru = $conn->query($sql);
while ($row = $ru->fetch_assoc()) {
echo '<tr>';
echo '<td>'.$row['owner_name'].'</td>';
echo '<td>'.$row['pos'].'</td>';
echo '<td>'.$row['owned'].'</td>';
echo '<td class="text-right">';
echo '<button class="button tiny">View User</button>';
echo '<button class="button alert tiny">Delete</button>';
echo '</td>';
echo '</tr>';
}
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.