如何在mysqli_fetch_assoc的while循环内仅打印所需的结果?

时间:2019-02-26 07:02:39

标签: mysql sql database mysqli mariadb

按照标题中的要求,我只想打印付款大于10的那些条目的结果,否则它将回显错误消息。在这里,我的代码确实打印了结果,但同时也打印了错误消息和结果。这对我来说很难解决该问题。我是新手,需要您的帮助。预先感谢

 <?php                              

            if(isset($_GET['l']))
                         {  
                            $user=$_SESSION['username'];
                            $qry1="SELECT * from entry where username='$user'";
                            $result1=mysqli_query($conn,$qry1);
                        if(!$result1)
                            {   
                            die("Sorry, Something went wrong. Please try again");
                            header("Location: index.php");
                            exit();
                            }
                        while ($row1 = mysqli_fetch_assoc($result1)) 
                            {   $name=$row1['sent_to'];
                                $time=$row1['time'];
                            if(($row1['payment']>10) && ($_GET['r']==$time) && ($_GET['l']==$name))
                                {
                                    echo $row1['entry']." ";
                                }
                            else
                                {   
                                    echo "error";
                                }
                            }
                         }
                  ?>

上面的代码显示结果以及错误消息,例如

entry1 errorerrorerrorerrorerrorerror

请检查我在哪里做错了

1 个答案:

答案 0 :(得分:0)

感谢大家的关注。我只是限制了数据库的while循环结果,它正在工作。如果有更好的方法,您的摘要将受到欢迎。

这是已解决的代码,可供参考

 if(isset($_GET['l']))
                         {  $name=$_GET['l'];
                            $time=$_GET['r'];
                            $user=$_SESSION['username'];
                            $qry1="SELECT * from entry where username='$user' and time='$time' and sent_to='$name'";
                            $result1=mysqli_query($conn,$qry1);
                        if(!$result1)
                            {   
                            die("Sorry, Something went wrong. Please try again");
                            header("Location: index.php");
                            exit();
                            }
                        while ($row1 = mysqli_fetch_assoc($result1)) 
                            {   $name=$row1['sent_to'];
                                $time=$row1['time'];
                            if(($row1['payment']>10) && ($_GET['r']==$time) && ($_GET['l']==$name))
                                {
                                    echo $row1['entry'];
                                }
                            else
                                {   
                                    echo "error";
                                }
                            }
                         }
                  ?>