简体   繁体   English

有关此PHP脚本的输入,以查询mysql数据库?

[英]Input regarding this PHP script to query a mysql database?

I was using this script and everything was great except for the fact that when the actual search was executed and "No Results" was the answer, I wanted the script to display such. 我正在使用此脚本,除了执行实际搜索并且答案为“无结果”时,其他一切都很棒,我希望脚本显示这样的事实。

When doing research to see where the FAIL was I discovered that I should be using MySQLi. 在进行研究以查看失败的位置时,我发现我应该使用MySQLi。 I have been at this script for 2 days and I seem to be getting further instead of closer. 我已经使用了2天的脚本,而且似乎越来越近了。 A little help here fellas? 小伙子们在这里有帮助吗?

What I am using: 我正在使用什么:

    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);

You can retrieve the count of the rows with: 您可以使用以下方法检索行数:

mysqli_num_rows($page_query);

Simply verify that it is >0 to chose what to display, the error message or the results 只需确认选择显示的内容,错误消息或结果>0

Just add a condition to display something if your query returns 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM