简体   繁体   English

php while循环附加条件不符合预期

[英]php while loop addtitional condition not behaving as expected

I have this piece of code: 我有这段代码:

        $i=0;
        while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){
*(line 133)*    if (array_search("SUCCESS", $row) != false) {
                echo "<tr class='success'>";
            }
            elseif (array_search("ERROR", $row) != false) {
                echo "<tr class='danger'>";
            }
            else {
                echo "<tr>";
            }
                foreach ($row as $rowData) {
                    if ($rowData == "SUCCESS") {
                        echo "<td><span class='mdi-navigation-check'></span></td>";
                    }
                    elseif ($rowData == "ERROR") {
                        echo "<td><span class='mdi-navigation-close'></span></td>";
                    }
                    else {
                        if (gettype($rowData) == "object") {
                            echo "<td>" . $rowData->format('Y-m-d H:i:s') . "</td>";
                        } else {
                            echo "<td>" . $rowData . "</td>";
                        }
                    }
                }
                if (isset($row["cMsgID"])) {
                    echo "<td><a href='#' onclick='window.open(\"/monitor/templates/history.php?cMsgID=" . $row["cMsgID"] . "\", \"history\", \"status=1, toolbar=1 width=800px, height=400px\")'>History</a></td>";
                }
            echo "</tr>";
            $i++;
        }

But when I add a second condition to the while loop the $row variable becomes a boolean true : 但是,当我在while循环中添加第二个条件时, $row变量变为布尔true

$i
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) && $i < 10){
*same code as above*}

First php error message: Warning: array_search() expects parameter 2 to be array, boolean given in C:\\wamp32bit\\www\\monitor\\templates\\mainContent.php on line 133 第一个php错误消息: 警告:array_search()期望参数2为数组,在第133行的C:\\ wamp32bit \\ www \\ monitor \\ templates \\ mainContent.php中给出布尔值

I have searched for this problem but all of them were related to wrong syntax or the conditions excluding each other. 我已经搜索了此问题,但是所有这些都与错误的语法或彼此排斥的条件有关。

Why does this happen? 为什么会这样?

Your problem here is just syntax, that way you are assigning always a boolean value to the $row. 您的问题只是语法,因此您总是向$ row分配布尔值。

$row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC) && $i < 10

The right way to do this is 正确的方法是

($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) && $i < 10

In the last case you can put an if statment inside the loop, like this: 在最后一种情况下,可以将if语句放入循环中,如下所示:

$i=0 while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){ if ($i < 10) { *do something* } else { break; } }

你为什么不试试这个?

while($i < 10){ while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)){ your code here; } }

Another way you could do it would be : 您可以做的另一种方式是:

$i=0;    
while (true == ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) && $i < 10) {
    *code*
    $i++;
}

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

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