【问题标题】:php, mysql, "Else after while statementphp, mysql, "Else 在 while 语句之后
【发布时间】:2012-10-09 06:05:08
【问题描述】:

我怎样才能写一个条件,如果没有找到该行然后做某事?

我的代码

$resultt = mysql_query("SELECT * FROM objednavky WHERE kdo = '$kdo' and kdy = '$the_rday'");
                        while ($roww = mysql_fetch_array($resultt)) {

                            echo "<form method='post' action=''>
                                  <label for='obb1'>Menu #1&emsp;</label>
                                  <input type='text' name='obb1' value='$roww[3]' size='4'><br>
                                  <label for='obb1'>Menu #2&emsp;</label>
                                  <input type='text' name='obb2' value='' size='4'><br>
                                  <label for='obb1'>Menu #3&emsp;</label>
                                  <input type='text' name='obb3' value='' size='4'><br>
                                  <input type='submit' value='objednat'></form>";


                        }

我想继续这样下去

   ...} else {
echo " no match ";
}

非常感谢您的帮助。

【问题讨论】:

标签: php mysql database phpmyadmin mysqli


【解决方案1】:

这可以通过mysql_num_rows()完成

 if($resultt && mysql_num_rows($resultt)>1){


             while ($roww = mysql_fetch_array($resultt)) {

                        echo "<form method='post' action=''>
                              <label for='obb1'>Menu #1&emsp;</label>
                              <input type='text' name='obb1' value='$roww[3]' size='4'><br>
                              <label for='obb1'>Menu #2&emsp;</label>
                              <input type='text' name='obb2' value='' size='4'><br>
                              <label for='obb1'>Menu #3&emsp;</label>
                              <input type='text' name='obb3' value='' size='4'><br>
                              <input type='submit' value='objednat'></form>";


                    }}  else {
                         echo " no match ";
                       }

但不推荐使用mysql_* 函数,因此请改用pdomysqli

【讨论】:

    【解决方案2】:

    在这种情况下你可以使用mysql_num_rows()

    $resultt = mysql_query("SELECT * FROM objednavky WHERE kdo = '$kdo' and kdy = '$the_rday'");
    
    
       if(mysql_num_rows($resultt)<1){
       echo "not found";
       }else{
    
         while(...)
    
           }
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2016-05-27
      • 2015-11-08
      • 2011-03-18
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 2012-06-18
      • 1970-01-01
      相关资源
      最近更新 更多