English 中文(简体)
如果质问不退还任何结果,如何停止执行文字
原标题:How to stop execution of the script if query does not return any result
  • 时间:2011-11-22 02:04:28
  •  标签:
  • php
  • mysql

我的网页上有多个<代码>company_id。 其中有些人没有返回任何结果,造成以下疑问。

我的问题是,我应该为他们做些什么? 如果找不到任何证据,我是否应当停止执行。 如果是,我如何能够这样做?

<?php
   $categories = mysql_query( SELECT distinct(category),id FROM products WHERE company_id =   . $cid );
   while($cat = mysql_fetch_assoc($categories)) 
   {
      echo "<a href= # >" . $cat[ category ] . "</a><br>";
   }
?>
最佳回答

我不鼓励“<条码>突破>,否则,你应检查一下,在登机之前,已经用<条码>归还的行文。

<?php
if(isset($cid)){
    $categories = mysql_query( SELECT distinct(category),id FROM products WHERE company_id =   . $cid );
    if(mysql_num_rows($categories)>=1){
        while($cat = mysql_fetch_assoc($categories)){
            echo "<a href= # >" . $cat[ category ] . "</a><br>";
        }
    }
}
?>
问题回答

停止在使用假声明中执行

类似情况

while($cat = mysql_fetch_assoc($categories)) 
{
   if ( $cat[ category ] ==    )
      break;                   
   echo "<a href= # >" . $cat[ category ] . "</a><br>";
}

keep in mind that this will stop the loop immediately. If you just want to "hide" empty results try 类似情况 this

while($cat = mysql_fetch_assoc($categories)) 
{
   if ( $cat[ category ])            
   echo "<a href= # >" . $cat[ category ] . "</a><br>";
}

利用BREAK来摆脱 lo。 但我感到困惑。 如果问询没有回过任何东西,那么你会不走任何路、正确吗?

http://www.ohchr.org。

它像你一样,通过一组公司——在你的网页上id,然后为每个公司打上分类?

foreach ($list_companies as $cid)
{
   LOAD CATEGORIES for $cid
   while (have categories)
   {
     echo link to it
   }
}

如果该权利被放弃,那么你就会在下一家公司时和上下一家公司,如果不存在任何类别,那么就没有任何东西可以打破。

始终行之有效的最便捷方式之一是die(><>>>>>功能。

if(empty($var)) {
    die();
}

or you can add an error message

die("No results found!");




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...