PHP-关联结果并在MYSQL中获取结果时遇到麻烦

问题描述:

所以在一个问题中=> 如何获取有针对性的结果与MYSQL相关?提出了有关如何获取相关结果的问题,我得到了一个答案,但是从那以后我改变了格式。

So in a question => How to fetch targeted results that are related in MYSQL? asked a question on how to fetch results that are related, I got an answer however I have since then changed the formatting.

对于我的项目,我有四个圆圈,这些圆圈将在他们的脸上显示一个数字,如下所示:

For my project I have four circles that will display a number on their face as seen here:

<div class="front" style="background: #4094ee;">
    100
</div>

现在我想使用这些数字,例如其中一个数字需要显示金额用户被禁止的次数(行)。如果用户在表中不存在(从未被禁止),则返回数字0。

Now I want to put those numbers to use, for example one of the numbers need to display the amount of times(rows) that a user has been banned. And if the user doesn't exist within the table(has never been banned) then return the number 0.

我得到了要显示的数字,但没有显示每个用户名正确的数字,它将继续显示表中的总行数,而不显示每个用户的行数(如果存在的话)

I got the number to display however it doesn't display the correct number per user name, it keeps displaying the total amount of rows in the table rather than the amount of rows per user if there are rows that exist

<?php
try {
    $stmt = $db->prepare('SELECT player_data.uuid, banned_players.uuid FROM  player_data, banned_players WHERE username = :username and player_data.uuid >=  banned_players.uuid');
    $stmt->execute(array(':username' => $_GET['name']));
   } catch(PDOException $e){
    //if an error is thrown a message will display
    echo $e->getMessage();
    exit();
}
?>
<?php if ($stmt->rowCount() <= 1 ): ?>
0
<?php else: ?>
   <?php while(false !== ($row = $stmt->fetch(PDO::FETCH_ASSOC))): ?>
   <?php endwhile; ?>
<?php endif; ?>

我需要使用的列是表中的

The Columns that I need to make use of are from table

   player_data:
       -uuid
       -username
   banned_players:
       -uuid(player_data needs to reference this for each player)
       -permanent(if the player is banned permanently this will return 0 or 1)
       -active(if the player's ban is active this will return 0 or 1)

如果可能的话,我想尽量避免过多或根本不修改数据库表。

And if it all possible I would like to try to keep from modifying the database tables too much or even at all.

其余表的设置方式是,每个用户均由其UUID而非用户名来引用。而且我正在尝试在每个表中为用户关联 UUID

The way the rest of the tables are setup is each user is referenced by their UUID rather than their username. And I am trying to associate the UUIDs in each of these tables for the users.

如前所述,我设法获得了一个数字显示,但不是我想要的数字。

As I mentioned previously I have managed to get a number to display but it is not the number that I want.

所以这是我拥有的php,非常混乱,我不确定如何解决它,会怎样是我要完成的任务的最佳解决方案?

So this is the php that I have it's very messy and I am not sure how to fix it, what would be the best solution for what I am trying to accomplish?

我也很抱歉,如果答案是显而易见的,我对PHP不太满意,因为我仍在学习。

Also I apologize if the answer is pretty obvious and clear, I am not too good with PHP, as I am still learning.

如果我正确理解您的 UUID 列,玩家的唯一标识符(),因此,如果两列相等,则两个表中的值都应联接到此列上>。

If I understand you correctly your UUID column is a unique identifier (key) for a player, thus the values from both tables should be JOINed on this column in the case that the two columns are equal.

鉴于这种理解,您进行查询毫无意义,因为您使用的是> = 进行比较,使它选择您想要的记录,但也记录您不做的事情不想。应该是:

Given that understanding, you query makes little sense because you are using >= for the comparison, making it select the records you want, but also records you do not want. It should be:

SELECT player_data.uuid, banned_players.uuid 
FROM  player_data, banned_players 
WHERE player_data.uuid = banned_players.uuid
AND player_data.username = :username 

也可能是

SELECT player_data.uuid, banned_players.uuid 
FROM  player_data 
JOIN banned_players ON (player_data.uuid = banned_players.uuid)
WHERE player_data.username = :username