www.webdeveloper.com
Results 1 to 10 of 10

Thread: problems making a site search engine

  1. #1
    Join Date
    Feb 2006
    Posts
    170

    problems making a site search engine

    Hi, this problem has got me totally stuck - i'm trying to make search results appear but my either sql or php doesnt like something

    i'm getting the error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\homepage\searchresults.php on line 31

    my code is

    PHP Code:
      while ($row mysql_fetch_array($result)) {
        
    $rowclass = ($rowclass == "row1"?"row2":"row1");
        echo 
    "<tr class=\"$rowclass\">\n<td>\n";
        
    $topicid=($row['topic_id']==0?$row['id']:$row['topic_id']);
        echo 
    "<p class=\"searchSubject\">\n<a href=\"viewtopic.php?t=" .
             
    $topicid "#post" $row['id'] . "\">" .
             
    $row['subject'] . "</a>\n";
        echo 
    "</p>\n";
        echo 
    "<p class=\"searchBody\">\n";
        echo 
    htmlspecialchars(trimBody($row['body']));
        if (
    $access_lvl 2) {
             echo 
    "<p>SQL: $sql</p>";
             echo 
    "<br><br>relevance: " $row['score'];
        } 
    line 31 is where the while loop starts

    cheers for any help

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    ok, so what happens before it?

    the issue is that "$result" is not a valid resource, meaning your query failed before it.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  3. #3
    Join Date
    Feb 2006
    Posts
    170
    heres the whole page

    PHP Code:
    <?php
    require_once 'conn.obj.php';
    require_once 
    'header.php';

    $result NULL;

    if (isset(
    $_GET['keywords'])) {
      
    $sql "SELECT *, MATCH (subject,body) " .
             
    "AGAINST ('" $_GET['searchwords'] . "') AS score " .
             
    "FROM objects " .
             
    "WHERE MATCH (description,keywords) " .
             
    "AGAINST ('" $_GET['searchwords'] . "') " .
             
    "ORDER BY score DESC";

      
    $result mysql_query($sql$conn)
        or die(
    'Could not perform search; ' mysql_error());
    }

    echo 
    "<table class=\"forumtable\" width=\"100%\" " .
         
    "cellspacing=\"0\">\n";
    echo 
    "<tr><th class=\"searchHeader\">Search Results</th></tr>\n";

    if (
    $result and !mysql_num_rows($result)) {
      echo 
    "<tr class=\"row1\"><td>No Learning Objects found that match the ";
      echo 
    "search term(s) '<strong>" $_GET['searchwords'] . "</strong>'";
      if (
    $access_lvl 2) echo "<p>SQL: $sql</p>";
      echo 
    "</td></tr>\n";
    } else {
      
    $rowclass "";
      while (
    $row mysql_fetch_array($result)) {
        
    $rowclass = ($rowclass == "row1"?"row2":"row1");
        echo 
    "<tr class=\"$rowclass\">\n<td>\n";
        
    $topicid=($row['topic_id']==0?$row['id']:$row['topic_id']);
        echo 
    "<p class=\"searchSubject\">\n<a href=\"viewtopic.php?t=" .
             
    $topicid "#post" $row['id'] . "\">" .
             
    $row['subject'] . "</a>\n";
        echo 
    "</p>\n";
        echo 
    "<p class=\"searchBody\">\n";
        echo 
    htmlspecialchars(trimBody($row['body']));
        if (
    $access_lvl 2) {
             echo 
    "<p>SQL: $sql</p>";
             echo 
    "<br><br>relevance: " $row['score'];
        }
        echo 
    "\n</p>\n";
        echo 
    "</td>\n</tr>\n\n";
      }
    }
    echo 
    "</table>";

    require_once 
    'footer.php';
    ?>

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    hmm you have some logic flaws in your code - if keywords is not set, you are still running everything. try adding near the top

    PHP Code:
    if(!isset($_GET['keywords'])){
    die(
    "invalid input");

    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  5. #5
    Join Date
    Feb 2006
    Posts
    170
    cheers looks like its sorted my code out but now i have a problem with my sql database

    now i get - Could not perform search; The used table type doesn't support FULLTEXT indexes

  6. #6
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    fulltext only works on MyISAM tables. Are you using MyISAM?
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  7. #7
    Join Date
    Feb 2006
    Posts
    170
    not sure im using mySQL 5 and the tables are created using the query browser gui

  8. #8
    Join Date
    Feb 2006
    Posts
    170
    i just changed the table type to myISAM

    then i just got - Could not perform search; Can't find FULLTEXT index matching the column list

    then a mate told me my sql query was wrong so now i've changed it too

    $sql = "SELECT * FROM objects WHERE Object_Name OR Description OR Keywords ORDER BY ObjectID DESC" ;

    which doesnt give any errors but it always returns no results

    any ideas?
    Last edited by cashton2k; 04-05-2006 at 03:43 PM.

  9. #9
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    you have to create a fulltext key on the columns in question if you want to use full text search (which is what you were doing this entire post)

    no reason to create a bunch of or's that don't compare to anything...

    http://dev.mysql.com/doc/refman/5.0/...ate-index.html
    you can use that to create the full text index that you want.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  10. #10
    Join Date
    Feb 2006
    Posts
    170
    cheers thanks for your help, got it going!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles