2017-09-28 47 views
0

這裏是一個數據庫的例子。我們把這個數據庫表Player在不同的表中將PHP mysql ID改爲用戶名

+---------+----------+-------------+------------+------------+--+ 
| Name | pid  | cash  | bankacc | Random  | | 
+---------+----------+-------------+------------+------------+--+ 
| carl | 123  | non   | 3434343434 | 34343433 | | 
| petter | 456  | non   | 3434343434 | 3434343434 | | 
|  sam | 1337  | non   | 3434343434 | 3434343434 | | 
|   |   | non   | 3434343434 | 3434343434 | | 
+---------+----------+-------------+------------+------------+--+ 

這裏是叫House另一個表;在房子只顯示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 | | 
+---------+----------+-------------+------------+------------+--+ 

我試圖顯示此表在我的PHP的網站稱爲「家」,但我不希望顯示的「PID」,而不是「PID」我想房主的名字。

$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>'; 
    } 
?> 

但這裏是一個查詢的例子,我想如何。真的很糟糕的例子。

$sql = "SELECT pid = name, pos, owned FROM houses, player"; 
+0

解釋一下你的問題。什麼是你傳遞的想要的。 –

+0

在你的sql語句中使用'INNER JOIN'。 –

+2

您需要閱讀SQL教程,而不是隨意編寫代碼。 – Barmar

回答

1

使用加入到從球員表得到名字

SELECT player.name, house.pos, house.owned 
FROM houses house 
LEFT JOIN player player on player.pid=house.pid 

代碼

$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的抓取行教程:https://www.w3schools.com/php/func_mysqli_fetch_row.asp

一些注意事項:

不如改列「名」到「OWNER_NAME」,以避免任何衝突或周圍使用的列名反引號(')當您在查詢中使用保留的關鍵字:

https://dev.mysql.com/doc/refman/5.7/en/keywords.html

0

謝謝你們,我得到它的工作: )

這是工作的代碼我如何改變它有點

$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>'; 
} 
相關問題