【问题标题】:PHP mysql ID to username in different tablesPHP mysql ID到不同表中的用户名
【发布时间】:2017-09-28 02:43:29
【问题描述】:

这是一个数据库的例子。我们称这个数据库表为Player

+---------+----------+-------------+------------+------------+--+
| 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 站点中显示这个名为“House”的表,但我不想显示“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";

【问题讨论】:

  • 详细说明您的问题。您中继想要的输出是什么。
  • 在你的sql语句中使用INNER JOIN
  • 你需要阅读SQL教程,而不是随便写代码。

标签: php html sql database mysqli


【解决方案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 fetch Row 教程:https://www.w3schools.com/php/func_mysqli_fetch_row.asp

一些注意事项:

当您在查询中使用保留关键字时,最好将列“名称”更改为“所有者名称”以避免任何冲突或在列名称周围使用反引号(`):

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

【讨论】:

    【解决方案2】:

    谢谢你们,我成功了:)

    这是我如何改变它的工作代码

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

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      相关资源
      最近更新 更多