doufuhuang6981 2015-07-21 13:32
浏览 50
已采纳

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

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:

$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

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?

  • 写回答

3条回答 默认 最新

  • dqtu14636 2015-07-21 13:45
    关注

    Your problem here is just syntax, that way you are assigning always a boolean value to the $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:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛