-Karl- Posted June 3, 2010 Share Posted June 3, 2010 function searchItem($name) { $arraySearch = explode(" ", $name); $table = 'items'; $countSearch = count($arraySearch); $b = 0; $query = "SELECT * FROM " . $table . " WHERE (`name` "; while ($b < $countSearch) { $query = $query."LIKE '%$arraySearch[$b]%')"; $b++; if ($b < $countSearch) { $query = $query." AND (`name` "; } $b = 0; } $query = $query.")"; $sql = mysql_query($query); That's the SQL part of my function. Anyway, the problem is I keep getting "Fatal error: Maximum execution time of 30 seconds exceeded in". I can't quite figure out why, hoping some new eyes can catch whatever I'm missing. Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/ Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 With setting $b to 0 at the end, you're causing an infinite loop. Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/#findComment-1067223 Share on other sites More sharing options...
-Karl- Posted June 3, 2010 Author Share Posted June 3, 2010 Oh wow, didn't notice both of them.. Now I'm getting: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource. $row = mysql_fetch_array($sql); if(!mysql_num_rows($sql)==0){ echo 'Random Echo'; } Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/#findComment-1067225 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 You most likely have an error in your SQL syntax. Look into mysql_error.. Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/#findComment-1067228 Share on other sites More sharing options...
-Karl- Posted June 3, 2010 Author Share Posted June 3, 2010 Fixed the query, it was adding a ). Anyway, I can't get my head around it only displaying the results if more than one result is found. It doesn't display if there's only one result. function searchItem($name) { $replace = str_replace($name, '+', ' '); $arraySearch = explode(" ", $name); $table = 'items'; $countSearch = count($arraySearch); $b = 0; $query = "SELECT * FROM " . $table . " WHERE (`name` "; while ($b < $countSearch) { $query = $query."LIKE '%$arraySearch[$b]%')"; $b++; if ($b < $countSearch) { $query = $query." AND (`name` "; } } echo $query; $sql = mysql_query($query); $row = mysql_fetch_array($sql); if(!mysql_num_rows($sql)==0){ echo '<table style="font-family: sans-serif; font-size:11;"> <tr> <td width="70px">Item ID</td> <td width="70px">Image</td> <td width="150px">Name</td> <td width="70px">Min Price</td> <td width="90px">Market Price</td> <td width="70px">Max Price</td> <td width="70px">Change</td> </tr>'; while ($arr = mysql_fetch_assoc($sql)) { if (strstr($arr['change'], '+')) { $fontcolor = "green"; } if (strstr($arr['change'], '-')) { $fontcolor = "red"; } if (!strstr($arr['change'], '+') && !strstr($arr['change'],'-')) { $fontcolor = "blue"; } if(mysql_num_rows($sql) > 0){ echo '<tr> <td>'.$arr['itemid'].'</td> <td><img src="images/'.$arr['itemid'].'.gif"></td> <td>'.$arr['name'].'</td> <td>'.$arr['min'].'</td> <td>'.$arr['market'].'</td> <td>'.$arr['max'].'</td> <td><font color="'.$fontcolor.'">'.$arr['change'].'</font></td> <td>'.time_since($arr['time']).'</td> <td><a href="index.php?update='.$arr['name'].'">Update</a></td> </tr>'; } } } else { grabData($name); echo 'Data added'; } } That's my entire code for the search function. Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/#findComment-1067233 Share on other sites More sharing options...
-Karl- Posted June 3, 2010 Author Share Posted June 3, 2010 Nvm, fixt! Link to comment https://forums.phpfreaks.com/topic/203766-mysql-search-for-multiple-words/#findComment-1067244 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.