0

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..

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.

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 /> &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>';
                        }
                        }
                    }
Learner
  • 7
  • 3
  • 1
    You shouldn't post pseudo code here. What you have written here and the final product could be completely different, so any answers will be invaluable. – GrumpyCrouton Aug 15 '17 at 15:15
  • Why don't you just _start_ with a `like` query? Otherwise you're going to do 2 queries to your database for no reason. – GrumpyCrouton Aug 15 '17 at 15:17
  • @GrumpyCrouton The actual code related to this is 200 lines long. and also when i run it gives me an internal error. Should i post it? – Learner Aug 15 '17 at 15:36
  • Yes. You should post it. – GrumpyCrouton Aug 15 '17 at 15:36
  • @GrumpyCrouton I did it. Can you please tell me if there are any syntax errors with if-else statements – Learner Aug 15 '17 at 15:42
  • Syntax error on line 172. Looks like you have too many closing braces? – GrumpyCrouton Aug 15 '17 at 15:45
  • I don't usually do this, and this was pretty hard to do, but I fixed your syntax issues [here (pastebin)](https://pastebin.com/7fbZf1Km). I had to go through your entire code and format it by hand to do this – GrumpyCrouton Aug 15 '17 at 16:05
  • @GrumpyCrouton Sir, I can't thank you enough for this. It is actually working now. Thank you for your time. You have really helped me a lot. – Learner Aug 15 '17 at 16:14
  • Oh, if it fixed the issue then I will post it as an answer. – GrumpyCrouton Aug 15 '17 at 16:14

2 Answers2

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 {).

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

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.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
0

If you could access the mysql.ini of your host, you could simply change the min word length for full text search.

[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.

One way around could also be to only use full text, if the user types more than 2 chars.

// ...
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.

Philipp
  • 15,377
  • 4
  • 35
  • 52