当Match Against函数没有在Mysql中给出任何结果时,LIKE函数会给我更好的结果吗?

时间:2017-08-15 15:11:00

标签: php mysql mysqli

目前,当我在搜索引擎中输入“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'}

当我在搜索引擎中输入“M”时,此代码是否会给出结果'Math'。

实际代码。

<?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 /> &nbsp &nbsp <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 个答案:

答案 0 :(得分:1)

您的代码中存在一些语法问题,您的其他语句未正确排列,因此您的页面无效。 (例如,您有一个} else {与另一个} else {对齐。

我浏览了所有代码并对其进行了格式化,以便我可以看到问题是什么,记住,一些合理的代码缩进将是一个好主意。 它可以帮助我们阅读代码,更重要的是它可以帮助调试代码Take a quick look at a coding standard为了您自己的利益。 您可能会被要求在几周/几个月内修改此代码,最后您会感谢我。

<?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 /> 
                    &nbsp &nbsp <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>';
    }
}

我还建议,对于非常复杂的嵌套结构,要注释每个结束括号的内容,就像我在代码中所做的那样。我发表评论,以便能够确切地看到}结束的内容。

  

警告: This code may be vulnerable to SQL Injection Attacks 。您应该了解Prepared StatementsMySQLiPDO。我推荐PDO,我wrote a function for使其非常简单,非常干净,以及更强安全使用非参数化查询。

答案 1 :(得分:0)

如果您可以访问主机的mysql.ini,则只需更改全文搜索的最小字长即可。

[mysqld]
ft_min_word_len=N

否则,您应该决定要使用哪个搜索功能。如果一个搜索是全文搜索而另一个搜索是简单的LIKE,则用户不是很直观。

如果用户输入的字符数超过2个,则单向处理也可能只使用全文。

// ...
if (strlen($query) < 4) {
    $where = "MATCH (...) AGAINST ...";
} else {
    $where = "...LIKE ... ";
}
$sql .= $where;

顺便说一下。 MATCH AGAINST应该正常返回比结果更多的结果,所以在MATCH AGAINST返回0行后进行LIKE搜索是有点无用的。