Author Topic: Mysqli db search engine problems  (Read 422 times)

mac041586

  • New Member
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
I have been trying to get this and many others like it to work. the issue is when I submit for the search I go to a blank page... I have worked out all the kinks other than this one and it has been reoccuring regardless of which search engine code I use... Someone please help!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
   
   
<?PHP
   
function getmicrotime()
   {
       list(
$usec$sec) = explode(" "microtime());
       return ((float)
$usec + (float)$sec);
   }
   
//initializing connection to the database 
   
$connection_string dirname(__FILE__) . "/connectionstring.php";
  require_once(
$connection_string);
  
//selecting table
  
mysqli_select_db($conn$DB_NAME)  or die ( 'Unable to select database.' );
  
//max number of results on the page
  
$RESULTS_LIMIT=10;
  if(isset(
$_GET['search_term']) && isset($_GET['search_button']))
  {
        
$search_term $_GET['search_term'];
      if(!isset(
$first_pos))
      {
          
$first_pos "0";
      }
      
$start_search getmicrotime();
        
//initializing mysqli Quary  
      
$sql_query mysqli_query("SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term')");
      
//additional check. Insurance method to re-search the database again in case of too many matches (too many matches cause returning of 0 results)
      
if($results mysqli_num_rows($sql_query) != 0)
              {
                  
$sql =  "SELECT * FROM news WHERE MATCH(title,article) AGAINST('$search_term') LIMIT $first_pos$RESULTS_LIMIT";
                    
$sql_result_query mysqli_query($sql);         
              }
      else
              {
                    
$sql "SELECT * FROM news WHERE (title LIKE '%".mysqli_real_escape_string($search_term)."%' OR article LIKE '%".$search_term."%') ";
                    
$sql_query mysqli_query($sql);
                    
$results mysqli_num_rows($sql_query);
                    
$sql_result_query mysqli_query("SELECT * FROM news WHERE (title LIKE '%".$search_term."%' OR article LIKE '%".$search_term."%') LIMIT $first_pos$RESULTS_LIMIT ");
              }
      
$stop_search getmicrotime();
        
//calculating the search time
      
$time_search = ($stop_search $start_search);
  }
  
?>
  <?PHP
  
if($results != 0)
  {
  
?>   
     <!-- Displaying of the results -->
  <table border="1" cellspacing="2" cellpadding="2">
    <tr>
      <td width="47%">Results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "?></td>
      <td width="53%" align="right" height="22">Results <b>
        <?PHP echo ($first_pos+1)." - ";
        if((
$RESULTS_LIMIT $first_pos) < $results) echo ($RESULTS_LIMIT $first_pos);
        else echo 
$results ?>
  
    </b>
        out of <b><?PHP echo $results?></b>
        for(<b><?PHP echo sprintf("%01.2f"$time_search); ?></b>)
        seconds </td>
    </tr>
    <tr>
      <form action="" method="GET">
        <td colspan="2" align="center"> <input name="search_term" type="text" value="<?PHP echo $search_term?>" size="40">
          <input name="search_button" type="submit" value="Search"> </td>
      </form>
    </tr>
    <?PHP   
      
while($row mysqli_fetch_array($sql_result_query))
      {
      
?>
  
      <tr align="left">
          <td colspan="2"><?PHP echo $row['title']; ?></td>
        </tr>
    <?PHP
      
}
      
?>
  
</table>
  <?PHP
  
}
  
//if nothing is found then displays a form and a message that there are nor results for the specified term
  
elseif($sql_query)
  {
  
?>
  
<table border="0" cellspacing="2" cellpadding="0">
      <tr>
          <td align="center">No results for   <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "?></td>
      </tr>
      <tr>
          <form action="" method="GET">
          <td colspan="2" align="center">
              <input name="search_term" type="text" value="<?PHP echo $search_term?>">
              <input name="search_button" type="submit" value="Search">
          </td>
          </form>
      </tr>
  </table>
  <?PHP
  
}
  
?>
  
<table width="300" border="0" cellspacing="0" cellpadding="0">
       <?php
       
if (!isset($_GET['search_term'])) { ?>
 
    <tr>
         <form action="" method="GET">
         <td colspan="2" align="center">
             <input name="search_term" type="text" value="<?PHP echo $search_term?>">
             <input name="search_button" type="submit" value="Search">
         </td>
         </form>
     </tr>
     <?php
       
}
       
?>
 
  <tr>
     <td align="right">
 <?PHP
 
//displaying the number of pages where the results are sittuated
 
if($first_pos 0)
 {
   
$back=$first_pos-$RESULTS_LIMIT;
   if(
$back 0)
   {
     
$back 0;
   }
   echo 
"<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$back' ></a>";
 }
 if(
$results>$RESULTS_LIMIT)
 {
   
$sites=intval($results/$RESULTS_LIMIT);
   if(
$results%$RESULTS_LIMIT)
   {
     
$sites++;
   }
 }
 for (
$i=1;$i<=$sites;$i++)
 {
   
$fwd=($i-1)*$RESULTS_LIMIT;
   if(
$fwd == $first_pos)
   {
       echo 
"<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '><b>$i</b></a> | ";
   }
   else
   {
       echo 
"<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '>$i</a> | ";   
   }
 }
 if(isset(
$first_pos) && $first_pos $results-$RESULTS_LIMIT)
 {
   
$fwd=$first_pos+$RESULTS_LIMIT;
   echo 
"<a href='search.php?search_term=".stripslashes($search_term)."&first_pos=$fwd ' > >></a>";
   
$fwd=$results-$RESULTS_LIMIT;
 }
 
?>
 
    </td>
   </tr>
 </table>
 


PHP Help Forum

Mysqli db search engine problems
« on: October 23, 2016, 12:25:50 am »


JimL

  • Master PHP Helper
  • Senior Member
  • *
  • Posts: 1729
  • Karma: 155
  • function deleteUsers(Vector<User> $users): void {}
    • View Profile
The only place success comes before work is in the dictionary. - Vince Lombardi

I won't help if you're using mysql_* functions | You should use PDO | MariaDB | Netbeans IDE | About me

PHP Help Forum

Re: Mysqli db search engine problems
« Reply #1 on: October 23, 2016, 04:00:06 am »