MySQL Search Error

Discussion in 'MySQL' started by Pudge1, Aug 16, 2010.

  1. #1
    
    function Search($keyword,$subject,$grade,$search_by)
     {
      $user = SQL_User();
      $pass = SQL_Pass();
      $dbname = SQL_DB();
    
      $connection = mysql_connect('localhost',$user,$pass);
    
      mysql_select_db($dbname , $connection);
    
      $search_term = mysql_real_escape_string($search_term);
    
      $sql = "SELECT * FROM Notes WHERE Keywords LIKE '%$search_term%'";
    
       if($subject == 'NO')
        {
        }
       else
        {
         $sql .= " AND Subject='$subject'";
        }
    
       if($grade == 'NO')
        {
        }
       else
        {
         $sql .= " AND Grade='$grade'";
        }
    
       if($sort_by == 'NO')
        {
        }
       elseif($sort_by == 'RATING')
        {
         $sql .= " ORDER BY Rating DESC";
        }
       elseif($sort_by == 'VIEWS')
        {
         $sql .= " ORDER BY Views DESC";
        }
       else
        {
        }
    
      $query = mysql_query($sql , $connection);
    
        while($arr = mysql_fetch_array($query))
         {
          $title = file_get_contents('notes/' . $arr['Note_ID'] . '.note');
          $title = explode('||TITL_E=',$title);
          $title = $title[1];
          $title2 = explode('=||',$title);
    
          $user = SQL_User();
          $pass = SQL_Pass();
          $dbname = SQL_DB();
    
          $search_term = mysql_real_escape_string($search_term);
    
          $connection = mysql_connect('localhost',$user,$pass);
    
          mysql_select_db($dbname, $connection);
    
          $sql = "SELECT * FROM Notes WHERE Note_ID='$note_id'";
          $query = mysql_query($sql , $connection);
          $query = mysql_fetch_array($query);
    
          $title = $query["Title"];
    
          $contents = $title2[1];
          $contents2 = wordwrap($contents, 80, ",", true);
          $contents2 = explode(',',$contents2);
          $contents2 = $contents2[0];
    
           if($contents2 == $contents)
            {
            }
           else
            {
             $contents = $contents . '...';
            }
    
          $results .= "<a id='search_title' href='view_note.php?id=" . $arr['Note_ID'] . "'>" . $title . "</a><br />";
          $results .= "<span id='search_contents'>" . $contents . "</span><br />";
          $results .= "<span id='search_author'>" . $arr['Author'] . "</span><br /><br /><br />|*|?|>|";
         }
    
         return $results;
      }
    
    Code (markup):
    For some reason I get these errors

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/***/functions.php on line 713

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/***/functions.php on line 713

    I've looked over this many times. I even took the SQL Statement ($sql) and put it through the SQL Command on PHPMyAdmin and replaced the variable with what I am using as a keyword and it works just fine. But I can't figure out quite what is wrong.

    Line 713 Begins with While($arr...
     
    Pudge1, Aug 16, 2010 IP
  2. suraj4u

    suraj4u Well-Known Member

    Messages:
    514
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #2
    You have not send the $search_term to Search() function

    So the query
    $sql = "SELECT * FROM Notes WHERE Keywords LIKE '%$search_term%'";
    Show's the warning

    To fix:
    function Search($keyword,$subject,$grade,$search_by, $search_term)

    You must send/get $search_term value

    Correct me if i was wrong
     
    suraj4u, Aug 16, 2010 IP