当通过PHP查询时,如果mysql数据库中没有结果,我可以排除HTML输出吗?

时间:2013-08-12 18:09:41

标签: php html mysql

简而言之,我有一个PHP输出(代码只是部分),当显示在网页上时需要进行超链接,但问题是,当没有结果时,它会超链接空白URL。如果在执行搜索时没有结果,是否仍然不显示HTML?

为了澄清,我需要将其应用于输出的多个部分。

以下是代码的具体部分。

if(isset($_POST['search'])) {

$searchq = $_POST ['search'];
$query = mysql_query ("SELECT * FROM HH_list2 WHERE Company LIKE '%$searchq%'") or die ("could not search"); //OT TO HERE !!!!!
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'Sorry but we did not find anything';
    }else{
        while($row = mysql_fetch_array($query)) {
            $Company = $row ['Company'];
            $Twitter_ID = $row ['Twitter_ID'];
            $Online_chat = $row ['Online_chat'];
            $Online_form = $row ['Online_form'];
            $Email_1 = $row ['Email_1'];
            $Email_2 = $row ['Email_2'];
            $Email_3 = $row ['Email_3'];

            $output .= '<div style="background-color:#ebebeb; padding: 2em;"> <h3>'.$Company.' </h3><br/> Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a><br/> <a href="'.$Online_chat.'" target="_blank">Online Chat</a> <br/> <a href="'.$Online_form.'" target="_blank">Online Form</a><br/>'.$Email_1.'<br/>'.$Email_2.'<br/>'.$Email_3.'</div>';


        }

}

}

1 个答案:

答案 0 :(得分:5)

只需使用empty()功能

即可
if(!empty($Twitter_ID)){
$output .= '<div style="background-color:#ebebeb; padding: 2em;"> <br/>
Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a>
</div>';
}

参考php empty()

关于你的第二个问题

if(isset($_POST['search'])) {

$searchq = $_POST ['search'];
$query = mysql_query ("SELECT * FROM HH_list2 WHERE Company LIKE '%$searchq%'") or die ("could not search"); //OT TO HERE !!!!!
$count = mysql_num_rows($query);
if ($count == 0) {
    $output = 'Sorry but we did not find anything';
    }else{
        while($row = mysql_fetch_array($query)) {
            $Company = $row ['Company'];
            $Twitter_ID = $row ['Twitter_ID'];
            $Online_chat = $row ['Online_chat'];
            $Online_form = $row ['Online_form'];
            $Email_1 = $row ['Email_1'];
            $Email_2 = $row ['Email_2'];
            $Email_3 = $row ['Email_3'];

            $output .= '<div style="background-color:#ebebeb; padding: 2em;">';
if(!empty($Company)){
 $output .= '<h3>'.$Company.' </h3><br/> ';
 if(!empty($Twitter_ID)){
 $output .= 'Twitter: <a href="http://www.twitter.com/'.$Twitter_ID.'" target="_blank">'.$Twitter_ID.'</a><br/>';
}
 if(!empty($Online_chat)){
  $output .= '<a href="'.$Online_chat.'" target="_blank">Online Chat</a> <br/> ';
}
 if(!empty($Online_form)){
 $output .= '<a href="'.$Online_form.'" target="_blank">Online Form</a><br/>';
}
 if(!empty($Email_1)){
 $output .=$Email_1.'<br/>';
}
 if(!empty($Email_2)){
 $output .=$Email_2.'<br/>';
}
 if(!empty($Email_3)){
 $output .=$Email_3;
}
 $output .='</div>';


        }

}

  

Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial