Currently, When I type 'M' in the search engine I do not get any results inspite of having a 'Math' value in my database. (I am using Match Against function of mysql on my website). So I decided to make a code that first runs a Match Against function and if no output is obtained, it again runs the search but this time using 'LIKE' function of my sql and if still no result is obtained it shows alert 'no values found'. the gist of code is somewhat like this..
目前,当我在搜索引擎中输入“M”时,尽管我的数据库中存在“数学”值,但我没有得到任何结果。 (我在我的网站上使用Match Against mysql的功能)。所以我决定制作一个首先运行Match Against函数的代码,如果没有获得输出,它再次运行搜索,但这次使用我的sql的'LIKE'函数,如果仍然没有得到结果,则显示alert'没有值发现”。代码的要点有点像这样..
mysql(Match against function)
If (mysqli_num_rows > 0)
{output}
elseif (mysqli_num_rows < 1)
{ mysqli (LIKE function)
then show output}
else {show alert'no results'}
Will this code give the result 'Math' when I enter 'M' in the search engine.
当我在搜索引擎中输入“M”时,此代码是否会给出结果'Math'。
The actual code.
实际的代码。
<?php
if (isset($_POST['go']))
{ $search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search))
{
if(isset($_POST['colleges']) )
{
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
}
else
{
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1) ;
$searchresults = mysqli_num_rows($query_run1);
?><p class = "totalresults"> <br />
<br />     <font size= "4" >Total Results :
<?php
echo $searchresults ;
?>
</font> </p>
<?php
if(mysqli_num_rows($query_run1)>0)
{
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
<?php
}
?>
</table>
<?php
}
elseif(mysqli_num_rows($query_run1) < 1)
{
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?> <table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12))
{
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px"> <?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username']; ?>
</td>
<td width="450px"> <a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php echo $descrip ; ?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php echo $rows['upload_date'] ; ?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php echo $college; ?>
</td>
</tr>
}
?>
</table>
<?php
}
else
{
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
else
{ echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
}
2 个解决方案
#1
1
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else {
lined up with another } else {
).
您的代码中存在一些语法问题,您的其他语句未正确排列,因此您的页面无效。 (例如,你有一个} else {排队另一个} else {)。
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end.
我仔细检查了所有代码并对其进行了格式化,以便我能够看到问题所在,请记住,一些合理的代码缩进将是一个好主意。它可以帮助我们阅读代码,更重要的是它可以帮助您调试代码。快速查看编码标准,以获得自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
    <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the }
was ending.
我还建议,对于非常复杂的嵌套结构,要注释每个结束括号的内容,就像我在代码中所做的那样。我发表评论,以便我可以确切地看到}结束了什么。
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend
PDO
, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.警告:此代码可能容易受到SQL注入攻击。您应该了解MySQLi或PDO的准备语句。我推荐PDO,我编写了一个函数,使其非常简单,非常干净,并且比使用非参数化查询更安全。
#2
0
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
如果您可以访问主机的mysql.ini,则只需更改全文搜索的最小字长即可。
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
否则,您应该决定要使用哪个搜索功能。如果一个搜索是全文搜索而另一个是简单的LIKE,则对用户来说不是很直观。
One way around could also be to only use full text, if the user types more than 2 chars.
如果用户键入超过2个字符,则可以使用全文。
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.
顺便说一句。 MATCH AGAINST应该正常返回比结果更多的结果,所以在MATCH AGAINST返回0行后进行LIKE搜索是有点无用的。
#1
1
You have some syntax issues in your code, your else statements were not lined up properly, so your page was not working. (For example, you had an } else {
lined up with another } else {
).
您的代码中存在一些语法问题,您的其他语句未正确排列,因此您的页面无效。 (例如,你有一个} else {排队另一个} else {)。
I went through all of the code and formatted it so I could see what the issue was, remember, Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help you debug your code. Take a quick look at a coding standard for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end.
我仔细检查了所有代码并对其进行了格式化,以便我能够看到问题所在,请记住,一些合理的代码缩进将是一个好主意。它可以帮助我们阅读代码,更重要的是它可以帮助您调试代码。快速查看编码标准,以获得自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。
<?php
if (isset($_POST['go'])) {
$search = $_POST['search'];
$college = $_POST['colleges'];
if (!empty($search)) {
if(isset($_POST['colleges']) ) {
$query1 = "Select filename, description, groupid, emailid, college, upload_date FROM images WHERE MATCH(description) AGAINST('$search') AND college = '$college' group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
} else {
$query1 = "Select filename, description, groupid, emailid, college,upload_date FROM images WHERE MATCH(description) AGAINST('$search') group by groupid order by MATCH(description) AGAINST('$search') DESC";
$query_run1 = mysqli_query($con, $query1);
$searchresults = mysqli_num_rows($query_run1);
?>
<p class = "totalresults">
<br />
<br />
    <font size= "4" >Total Results: <?php echo $searchresults; ?></font>
</p>
<?php
if(mysqli_num_rows($query_run1)>0) {
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run1)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
<?php
} //end while loop
?>
</table>
<?php
} else if(mysqli_num_rows($query_run1) < 1) {
$query12 = "Select * FROM images WHERE description LIKE '%".mysqli_real_escape_string($con,$search)."%' group by groupid order by groupid DESC";
$query_run12 = mysqli_query($con, $query12) ;
?>
<table class="searchenginelist" style="color: black;">
<tr>
<th> Uploader </th>
<th> Title </th>
<th> Date </th>
<th> Upvotes </th>
<th> Downvotes </th>
<th> College </th>
</tr>
<?php
while( $rows = mysqli_fetch_assoc($query_run12)) {
//$imagedisplay = $rows['file'];
$imagename = $rows['filename'];
// mysqli_real_escape_string($con,$imagedisplay);
$descrip = $rows['description'];
$groupid = $rows['groupid'];
//$groupid = $_SESSION['groupid'] ;
$uploader_emailid = $rows['emailid'];
$college = $rows['college'];
?>
<tr>
<td width="70px">
<?php
$query = " SELECT username from userinfo WHERE emailid = '$uploader_emailid' " ;
$query_run = mysqli_query($con,$query );
$raw = mysqli_fetch_assoc($query_run);
echo $raw['username'];
?>
</td>
<td width="450px">
<a href="imagespace.php?groupid=<?php echo $groupid ;?>">
<?php
echo $descrip ;
?>
</a>
</td>
<td style="font-size:15px;" width="85px">
<?php
echo $rows['upload_date'] ;
?>
</td>
<td align="center" style="color:green;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '1' ";
$query_run= mysqli_query($con, $query);
$upvote=mysqli_num_rows($query_run) ;
echo $upvote;
?>
</td>
<td align="center" style="color:red;" width="50px">
<?php
$query = "SELECT vote from votes where groupid = '$groupid' and vote = '0' ";
$query_run= mysqli_query($con, $query);
$downvote=mysqli_num_rows($query_run) ;
echo $downvote;
?>
</td>
<td style="font-size:13px;" width="130px">
<?php
echo $college;
?>
</td>
</tr>
</table>
<?php
} //end while loop
} else { //num rows < 1
echo '<script type="text/javascript"> alert ("No results found") </script>' ;
}
}
} else { //no searches returned
echo '<script type="text/javascript"> alert ("No data entered") </script>';
}
}
I also suggest, for really complex, nested structures, to comment what each ending bracket goes to like I did in your code. I put comments so that I could see exactly what the }
was ending.
我还建议,对于非常复杂的嵌套结构,要注释每个结束括号的内容,就像我在代码中所做的那样。我发表评论,以便我可以确切地看到}结束了什么。
WARNING: This code may be vulnerable to SQL Injection Attacks. You should Learn about Prepared Statements for MySQLi or PDO. I recommend
PDO
, which I wrote a function for to make it extremely easy, very clean, and way more secure than using non-parameterized queries.警告:此代码可能容易受到SQL注入攻击。您应该了解MySQLi或PDO的准备语句。我推荐PDO,我编写了一个函数,使其非常简单,非常干净,并且比使用非参数化查询更安全。
#2
0
If you could access the mysql.ini of your host, you could simply change the min word length for full text search.
如果您可以访问主机的mysql.ini,则只需更改全文搜索的最小字长即可。
[mysqld]
ft_min_word_len=N
Otherwise you should decide which search function you want to use. It's not very intuitive for users if one search is a full text search and the other is a simple LIKE.
否则,您应该决定要使用哪个搜索功能。如果一个搜索是全文搜索而另一个是简单的LIKE,则对用户来说不是很直观。
One way around could also be to only use full text, if the user types more than 2 chars.
如果用户键入超过2个字符,则可以使用全文。
// ...
if (strlen($query) < 4) {
$where = "MATCH (...) AGAINST ...";
} else {
$where = "...LIKE ... ";
}
$sql .= $where;
Btw. MATCH AGAINST should normaly return way more results than like, so doing a LIKE search after MATCH AGAINST returns 0 rows, is kinda useless.
顺便说一句。 MATCH AGAINST应该正常返回比结果更多的结果,所以在MATCH AGAINST返回0行后进行LIKE搜索是有点无用的。