2012-06-06 34 views
0

我花了数小时试图做这项工作。搜索解决方案没有帮助我,所以我终于问。Mysql_fetch_object()SQL查询(如何:如果找不到结果,显示“消息”)?

我试图做的是:如果没有结果发现显示的消息。

这就是我所拥有的,它目前并不令我感到沮丧。

$sql = mysql_query("SELECT * FROM cover WHERE MATCH(keyword,description,categories) AGAINST('$search_key' in boolean mode) ORDER BY id DESC LIMIT $from , $perPage"); 

    $i=1; 
    while($result = mysql_fetch_object($sql)) { 

    $keyword = $result->keyword; 
    $img = $result->img; 
    $description = $result->description; 

    if($img != null) { DO THIS; 
    if($i==2) { require_once(dirname(__FILE__).'/page.php'); } 
    $i++; } 
    } 
    if($img == null) { print "<center>No Results Found</center>"; } 

我不知道如何使用mysql_num_rows这个(这是我所看到的是网站上最流行的解决方案),它会返回一个错误,我想它不” t与mysql_fetch_object($sql)很好地工作?而我不认为这个:$img = $result->img;也可以与mysql_num_rows一起使用。

我只是试图找到一种方法来做到这一点,而无需修改括号内的任何内容,只是外面的。


当我说“不工作”,我的意思是它并不表明是为了显示,如果没有找到结果的消息。我已经测试了if($img != null) { print "<center>No Results Found</center>"; },它工作正常,它显示了消息。但它似乎不能以另一种方式工作,现在我只是感到困惑。

回答

0

下面的工作:

$sql = mysql_query("SELECT * FROM cover WHERE MATCH(keyword,description,categories) AGAINST('$search_key' in boolean mode) ORDER BY id DESC LIMIT $from , $perPage"); 
if(mysql_num_rows($sql) > 0) 
{ 
    $i=1; 
    while($result = mysql_fetch_object($sql)) 
    { 
     $keyword = $result->keyword; 
     $img = $result->img; 
     $description = $result->description; 

     if($img != null) 
     { 
      DO THIS; 
      if($i==2) 
      { 
       require_once(dirname(__FILE__).'/page.php'); 
      } 
      $i++; 
     } 
    } 
} 
else 
{ 
    print "<center>No Results Found</center>"; 
} 

我看不出有任何理由,你为什么会想要将其值从$result存储到独立您可以直接简单使用$result->keyword

+0

谢谢加文,这是**正是我正在寻找**!我发现了一种通过@Kasyx方法获得我想要的东西的不同方式,但这是我的问题所在,并且您已经回答了这个问题。关于使用'$ result->关键字'时使用不同的变量,以及我以这种方式购买了脚本,并且我只是不想再经过或修改括号内的大部分内容。感谢您的时间! – Mafia

+0

很高兴听到它帮助你。 ;) – Gavin

0

您可以使用它像这样

if(mysql_num_rows($result) <= 0); 
    { 
    echo 'There is no result'; 
    } 
    else 
    { 
     your code 
    } 

下面是这种在你的代码

$sql = mysql_query("SELECT * FROM cover WHERE MATCH(keyword,description,categories) AGAINST('$search_key' in boolean mode) ORDER BY id DESC LIMIT $from , $perPage"); 

    $i=1; 
    $result = mysql_fetch_object($sql); 
    if(mysql_num_rows($result) > 0) 
    { 
    while($result) { 

    $keyword = $result->keyword; 
    $img = $result->img; 
    $description = $result->description; 

    if($img != null) { DO THIS; 
    if($i==2) { require_once(dirname(__FILE__).'/page.php'); } 
    $i++; } 
    } 
    if($img == null) { print "<center>No Results Found</center>"; } 
    } 
    else 
    { 
     echo 'There is no result'; 
    } 
+0

它应该是'0 === mysql_num_rows($ sql)' – Lumbendil

+0

@Pramod,谢谢,但是我不认为这样可以工作:'$ img = $ result-> img;',并且我用了很多,代码在括号内非常庞大,所以我不想再碰到任何东西。我只是想找到一种方法来使这些括号外面的工作。 – Mafia

+0

请纠正双美元符号 –

1

执行。如果你有疑问与mysql_num_rows看看你的索引:

$i = 1; 

如果没有结果,循环后的值仍然是1,所以你需要检查AFTER while循环:

if($i == 1) { print "<center>No Results Found</center>"; } 
+0

谢谢@Kasyx,这个工作!我之前也尝试过这样做,但无法弄清楚方法,我正在尝试'if($ 1 == 0)'。感谢你,终于把这件事情从我的脑海中解救出来。 **:o)** – Mafia

+1

我很高兴,它帮助:) – Kasyx

0

你的问题在于,while()循环只发生在结果被返回的情况下,所以NULL比较永远不会在这里产生。

我建议你需要重新排序的代码如下所示:

$sql = mysql_query("SELECT * FROM cover WHERE MATCH(keyword,description,categories) AGAINST('$search_key' in boolean mode) ORDER BY id DESC LIMIT $from , $perPage"); 
$numResults = mysql_num_rows($sql); 
if ($numResults === 0) { // mysql_query returned no results 
    print "<center>No Results Found</center>"; 
} else { 
    require_once(dirname(__FILE__).'/page.php'); 
    while($result = mysql_fetch_object($sql)) 
    { 
     $keyword = $result->keyword; 
     $img = $result->img; 
     $description = $result->description; 
    } 
} 
相关问题