如果列中有选定的单词,则输出数据库信息


Issues outputting database information if a column has a selected word

我试图从我的数据库输出信息,只有当记录在列'状态'中填写'Pending'时。到目前为止,这是打破,我有多个记录与'Pending'填充,在那一栏。

我做错了什么?

<?php
$con = mysqli_connect("localhost", "root", "", "db");
$run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC");
$numrows = mysqli_num_rows($run);
    if( $numrows > 0) {
        while($row = mysqli_fetch_assoc($run)){
            $pending_id = $row['status'];
                if($pending_id == ["Pending"]){
                    $pending_id = $row['user_id'];
                    $pending_firstname = $row['firstname'];
                    $pending_lastname = $row['lastname'];
                    $pending_username = $row['username'];
                }
        }
    }
                echo $pending_firstname;
?>
更新:

我只得到第一个结果…

if( $numrows > 0) {
        while($row = mysqli_fetch_assoc($run)){
            $pending_id = $row['status'];
                if($pending_id == "Pending"){
                    $pending_id = $row['user_id'];
                    $pending_firstname = $row['firstname'];
                    $pending_lastname = $row['lastname'];
                    $pending_username = $row['username'];
                }
        }
    }
    if ($pending_firstname == true) {
                    echo $pending_firstname;
                } else {
                    echo "There are no Pending Requests as this time.";
                }
  ?>

不需要在方括号中放入字符串来匹配。

if($pending_id == "Pending")...

OK -试试这个代码…
你需要在循环中输出你的用户详细信息,否则它们将被覆盖

if( $numrows ) {
    while($row = mysqli_fetch_assoc($run)){            
        if($row['status'] == "Pending"){
            $pending_id        = $row['user_id'];
            $pending_firstname = $row['firstname'];
            $pending_lastname  = $row['lastname'];
            $pending_username  = $row['username'];
            echo "$pending_firstname $pending_lastname $pending_username <br>";
        }
    }
}

看你声明pending_firstname和所有变量到if语句下的while和另一个if语句。并且你在它的作用域之外重复pending_firstname。这就是为什么你会得到这个错误。为了避免这种情况,首先在全局范围内声明所有变量,然后您可以从任何地方访问它们。这也是一个很好的练习!

实际上你应该将所有变量声明为数组。使用array_push()方法,您可以将多个数据推入该数组,并且您可以在将来使用foreach迭代这些数组。