Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果列具有选定的字,则会输出数据库信息_Php_Html_While Loop - Fatal编程技术网

Php 如果列具有选定的字,则会输出数据库信息

Php 如果列具有选定的字,则会输出数据库信息,php,html,while-loop,Php,Html,While Loop,我正试图从数据库中输出信息,只有在“状态”列中填写了“待定”记录时才这样做。截至目前,这是打破,我有多个记录与'待定'填写,在该栏 我做错了什么 <?php $con = mysqli_connect("localhost", "root", "", "db"); $run = mysqli_query($con,"SELECT * FROM user_requests ORDER BY id DESC"); $numrows = mysqli_num_rows($run); i

我正试图从数据库中输出信息,只有在“状态”列中填写了“待定”记录时才这样做。截至目前,这是打破,我有多个记录与'待定'填写,在该栏

我做错了什么

<?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($pending_id == "Pending")...
确定-尝试此代码。
您需要在循环中输出您的用户详细信息,否则它们将被重写

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>";

        }
    }
}
if($numrows){
而($row=mysqli_fetch_assoc($run)){
如果($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
”; } } }
看,您在while和另一个if语句下声明了
pending_firstname
和所有变量。您正在回显其范围之外的
pending_firstname
。这就是为什么会出现这个错误。为了避免这种情况,首先声明全局范围内的所有变量,然后可以从任何地方访问它们。这也是一个很好的练习

更新


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

它仅在运行
while
if
块时定义。另外,比较
$pending\u id=[“pending”]
最多只能将字符串与数组进行比较。顺便说一句,为什么不在SQL查询中编码该过滤器?toy是什么意思“为什么顺便说一句,为什么不在SQL查询中编码该过滤器?”例如
WHERE status='Pending'
Gotcha。因为我将要做类似的事情….
$Pending\u id=$row['status'];if($Pending\u id='Pending”){
...
$pending_id=$row['status'];if($pending_id==“已批准”){
...
$pending_id=$row['status'];if($pending_id==“已拒绝”){
我最初有这个,但更改了它以查看它是否有用。我不再收到未定义变量通知,但仍然没有显示任何内容。我更新了我的问题。我只获得了第一条显示在回显中的记录,即使我在定义变量时有一个while循环?我更新了我的问题。我只获得了第一条显示在dis中的记录在我的echo中播放,即使我在定义变量时有一个while循环?很抱歉,我看到您的问题已经解决。实际上,您应该在全局范围内将所有需要的变量声明为数组,并使用
array\u push()
method您可以将多个值推入数组,将来您可以使用
foreach
或其他方法对它们进行迭代。您能给我举一个示例说明您的意思吗?我不太确定我是否理解了您的意思?
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>";

        }
    }
}