Jump to content

MySql, search for multiple words.


-Karl-

Recommended Posts

    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

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';

            }

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.