简体   繁体   English

mysql 的 while 循环中的 if 语句永远不是真正的 PHP

[英]If-statement in while loop with mysql is never true PHP

I am having trouble with a if-statement in a while loop i PHP.我在 PHP 的 while 循环中使用 if 语句时遇到问题。 I have an array with teams named $all_teams.我有一个名为 $all_teams 的团队的数组。 Let's say that it contains Team Blue, Team Red and Team Yellow.假设它包含蓝队、红队和黄队。 I also have a database where objects are assigned to teams.我还有一个数据库,其中对象被分配给团队。

DatabaseID | State |  Team | ... 
12           Ready    Blue
33           Finished Red
65           Ready    Blue

I am now trying to run the following code:我现在正在尝试运行以下代码:

 <?php
$query = "SELECT DISTINCT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' ORDER BY `Team` DESC  ;";
                        $select_projects = mysql_query($query);
    foreach($all_teams as $team){
        echo '<tr id = '.$team.'><td>'.$team.'</td><td>';
            while($all_objects = mysql_fetch_array($select_projects)){
                if($all_objects['Team'] == $team){
                    echo ''.$all_objects['DatabaseID'].'</br>';
                }
            }
        echo '</td></tr>';

    }
    ?>

I am trying to creat a table that should look like this.我正在尝试创建一个看起来像这样的表。

Blue   | 12
         65
Red    | 33
Yellow |

The if-statment if 语句

if($all_objects['Team'] == $team)

is never true... Why?永远不会是真的...为什么? How can I fix my problem?我该如何解决我的问题?

Resetting the result set pointer after each loop (and for now ignoring that you are using the deprecated mysql_* functions):-在每次循环后重置结果集指针(现在忽略您正在使用已弃用的 mysql_* 函数):-

<?php
$query = "SELECT DISTINCT * 
            FROM `dmt_objects` 
            WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' 
            AND Team IN ('".implode("','", $all_teams)."')
            ORDER BY `Team` DESC  ;";
if ($select_projects = mysql_query($query))
{
    foreach($all_teams as $team)
    {
        echo "<tr id = '$team'><td>$team</td><td>";
        while($all_objects = mysql_fetch_assoc($select_projects))
        {
            if($all_objects['Team'] == $team)
            {
                echo ''.$all_objects['DatabaseID'].'</br>';
            }
        }
        echo '</td></tr>';
        mysql_data_seek($select_projects, 0);
    }
}
else
{
    die(mysql_error());
}
?>

WARNING DON'T USE mysql_ use mysqli_ or PDO instead警告不要使用mysql_使用mysqli_PDO代替
Read this carefully: mysql_ deprecated仔细阅读:不推荐使用mysql_

<?php
$query           = "SELECT DISTINCT * FROM `dmt_objects` WHERE PipelineAndWorkflow = 'RWP WF  Ready for 4Sprint' ORDER BY `Team` DESC  ;";
$select_projects = mysql_query($query);
$all_objects     = mysql_fetch_array($select_projects)

foreach($all_teams as $team){
    echo '<tr id = '.$team.'><td>'.$team.'</td><td>';
        foreach($all_objects as $obj)
            if($obj['Team'] == $team){
                echo ''.$obj['DatabaseID'].'</br>';
            }
        }
    echo '</td></tr>';

}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM