簡體   English   中英

如何在選擇不同的查詢上應用條件

[英]how to apply condition on select distinct query

我在PHP中制作了一個從0到99在屏幕上創建隨機數的網頁。我將其存儲在數據庫中,然后通過選擇不同的查詢來獲取它,因為我不想重復這些數字。問題是我想回聲一些或未選擇任何唯一數字時顯示警報框。 我無法弄清楚..請有人幫助我..

<?php
$con=mysqli_connect('localhost','root','','bingo');
   if(isset($_POST['btn1'])){
        $sqli_del="DELETE FROM `numbers`";
       $que=mysqli_query($con, $sqli_del);
      if($que){
      echo "ALL NUMBERS ARE DELETED!!!!";
      }
     else{
      echo 'There is somethin wrong with the query...';
     }

     }
   if(isset($_POST['btn'])){
  $var=mt_rand(0,99);

    $sql="INSERT INTO `numbers`(`number`) VALUES ('$var')";
     $query=mysqli_query($con, $sql);

    $sqli="SELECT DISTINCT number FROM `numbers`";
     $res=mysqli_query($con, $sqli);


     }


    ?>

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     <title>Bingo</title>
      <style>
     .main{margin:0 auto;}
     .form{  margin: auto;
       width: 71%;}
    .form input{width:90px;
      height:50px;}
         .spin{background-color:green; color:white; border-radius:10px;font-
       weight:bold;}
     .del{background-color:red; color:white; border-radius:10px;font- 
    weight:bold;}
       .number{ 
    width: 110px;
       height: 107px;
      font-size: 98px;
       color: blue;
        background-color: black;
       padding-left: 22px;
   margin-left: 25px;}
   .left{float:left;}
   .right{float:left;padding:250px;}
  a{text-decoration:none;}
    .back{text-align:left !important;float:left;color:blue;}
     .button{float:left;padding-top:20px;}
     </style>
   </head>

  <body>
    <div class="main">
     <div class="form">
    <form method="POST" action="">
    <div class="left">
    <?php if(!empty($res)) {?>
     <div class="back">
    <?php
      $count=mysqli_num_rows($res);
     echo "(".$count.' '. 'numbers are printed..'.")"."<br>";

        while($row=mysqli_fetch_assoc($res)){
     $num=$row['number'];

      echo $num."<br>";}


     ?>
    </div>
    <?php } ?>
    </div>

     <div class="right">
     <?php if(!empty($num)) {?>

     <div class="number"> 
     <?php 
       echo $num ."<br>";?>
       </div>
     <?php } ?>
         <div class="button">
      <a href=""><input class="spin" type="submit" name="btn" value="Spin">           
      </a>
     <a href=""><input class="del" type="submit" name="btn1" value="Delete         
    All"></a>
     </div>
      </div>
    </form>
    </div>

    </div>
  </body>
    </html>

根據你的代碼

    <?php if(!empty($res)) {?>
       <div class="back">
       <?php
       $count=mysqli_num_rows($res);
         echo "(".$count.' '. 'numbers are printed..'.")"."<br>";
         while($row=mysqli_fetch_assoc($res)){
           $num=$row['number'];
           echo $num."<br>";
         }
    ?>
    </div>
    <?php
    } else {//empty, $res is false
        if(isset($_POST['btn'])){ //check whether post request
           echo "<span>no number</span>";
        }
    }

mysqli查詢

if ( false === $res) {

} else {

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM