只显示div,如果mysql结果显示8结果?

时间:2013-12-08 22:51:02

标签: php mysql

我试图使用count方法仅在mysql查询显示8个结果时回显div,否则如果显示7或更少则不显示此div。

继承人我试图做的事可以请某人告诉我我哪里出错了谢谢:

<?php
//PHP CODE STARTS HERE

if(isset($_GET['dosearch'])){

// Change the fields below as per the requirements
$db_host="*********";
$db_username="******";
$db_password="******";
$db_name="*****";
$db_tb_atr_name="******";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);


$query_for_result=mysql_query("SELECT *,MATCH(display_name, user_location, sexual_orientation, user_ethnicity, preferred_role, local_station, user_size, weight_st, weight_lb, height_ft, height_in, user_build, user_age) AGAINST ('$query' IN BOOLEAN MODE) AS relevance FROM ptb_stats, ptb_users WHERE ptb_stats.user_id=ptb_users.user_id AND ptb_users.account_type=\"user\" AND MATCH(display_name, user_location, sexual_orientation, user_ethnicity, preferred_role, local_station, user_size, weight_st, weight_lb, height_ft, height_in, user_build, user_age) AGAINST('$query' IN BOOLEAN MODE) ORDER BY relevance DESC LIMIT 8");
echo "<div class=\"search-results\"><div id=\"content2_header\">Members Matching Your Search</div>";
$platinum_count = mysql_num_rows($query_for_result);
while($data_fetch=mysql_fetch_array($query_for_result))

{

    echo"<div class=\"image_case\"><a href=\"profile.php?id={$data_fetch['user_id']}\"><img width=93px height=93px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\"></a>
<div id=\"mod_newest_text12\">{$data_fetch['display_name']}</div>
<div id=\"mod_newest_text14\">{$data_fetch['user_location']}</div></div>
";


}
// only if there are more than 8 users do we want to show our div
    if($platinum_count > 7){
        // how many default spaces do we need?
        $default_profiles_needed = 7 - $platinum_count;        
        for($i = 1; $i <= $default_profiles_needed; $i++){
            echo "<div class=\"morebutton-search21\"><a href=\"search_results.php?query=$query\" class=\"more\">+ view more results</a></div>";
        }
    }


mysql_close();
}

?>

2 个答案:

答案 0 :(得分:0)

请参阅mysqli for beginners

然后只需使用$result->num_rows;来计算结果。

答案 1 :(得分:0)

首先,不要使用mysql_ *函数,因为它们是deprecated

请考虑使用PDO(最好)或mysqli。请参阅this了解原因,以及一些代码示例。

现在让我们来看看你的代码,用PDO重新构想:

if(isset($_GET['dosearch'])){

    // Change the fields below as per the requirements
    $db_host="*********";
    $db_username="******";
    $db_password="******";
    $db_name="*****";
    $db_tb_atr_name="******"; // what is this?

    $dbh = new PDO("mysql:host=$db_host;dbname=$db_name", $db_username, $db_password);

    $query = //I can't read your query, but you should convert it to a prepared statement with ? or :id formats

    $stmt = $dbh->prepare($query);
    $result = $stmt->execute($_GET["query"]); // PDO handles escaping in prepared statement; put your data into the prepared statement's parameters

获取结果,计算逻辑:

    if($row = $result->fetchAll()) {
        // do stuff if results

        if(count($row) >= 8){
            // do stuff if 8 or more results
        }
    }
?>

这只是PDO的一些粗略代码,该链接提供了更多示例,例如在try / catch块中进行连接并捕获错误。