2

我正在使用这个脚本,一切都很好,除了当执行实际搜索并且“没有结果”是答案时,我希望脚本显示这样的事实。

在进行研究以查看失败的位置时,我发现我应该使用 MySQLi。我已经在这个脚本上呆了 2 天,而且我似乎越来越远而不是越来越近。小伙子们有一点帮助吗?

我正在使用什么:

    if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';

//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);

$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";

$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));

while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li> 
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}

echo $datarow;
echo "<br />";

include 'dbclose.php';
}

mysql_close($con);
4

2 回答 2

1

您可以使用以下方法检索行数:

mysqli_num_rows($page_query);

只需验证是>0选择要显示的内容、错误消息还是结果

于 2012-11-26T13:12:23.140 回答
0

如果您的查询返回 0,只需添加一个条件以显示某些内容

      if(empty($_GET['query'])){
header ("Location: /same_page");
}
else{
//connect
include 'connection_script.php';

//Get the "Term" from the search box
$query=mysql_real_escape_string($_GET['query']);

$page_str = "SELECT * FROM $tblname WHERE name like '%$query%' or clan like '%$query%'";

$page_query = mysqli_query($con,$page_str)or die(mysql_error($con));

if (mysqli_num_rows($page_query) > 0){

while($page_result = mysqli_fetch_assoc($page_query)){$datarow .= " <ul>
<li>Banned player : <a target='_blank' href=\"http://path/tosomething/here=" . $page_result[name] . " \">" . $page_result[name] . "</a></li>
<li>Clan Name : " . $page_result[clan] . "</li> 
<li>Reason : " . $page_result[reason] . "</li>
<li>Posted By : " . $page_result[moderator] . "</li>
<li>Date & Time : " . $page_result[dateandtime] . "</li>
<li>Evidence : <a target='_blank' href=\"$page_result[evidence]\">Here</a></li>
</ul><br />";
}

echo $datarow;
echo "<br />";
} else {
echo 'Your search returned 0 results';
}

include 'dbclose.php';
}

mysql_close($con);
于 2012-11-26T13:17:21.733 回答