Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem with for... else... statement for searching database

    Hello all,

    The following script is meant to check whether a form has been filled in and submitted and if so retrieve rows from a database that match the keywords submitted. Most of it works fine but if i enter a keyword that i know for sure is not present in the database entries the final else (between the stars) does not execute and instead the code that executes when a keyword is found in the database entries (i.e. when $query_result !== FALSE) is again executed. I suspect the problem is the
    PHP Code:
    if($query_result !== FALSE
    statement itself. It would seem as if i'm using the wrong method to check whether the query has been successful. I've also tried
    PHP Code:
    if($query_result
    but that didn't work either.

    Can anyone see what the problem is:
    PHP Code:
    <?php
    //check whether form has been submitted or sbmitted without filling in search field
    if (!$HTTP_POST_VARS || empty($_POST["keywords"]))
        {
    ?>
    <form method="post" action="<?php $_SERVER['PHP_SELF'?>">
    <h4>SEARCH</h4>
    <fieldset>
    <label for="keywords">Keywords: <em>(required - Case Sensitive)</em></label><br /><input id="keywords" name="keywords" type="text" size="30" /><br />
    </fieldset>
    </form>
    <?php 
        
    }
    //else executed only if form has been submitted and search field is not empty
        
    else
        {
        echo 
    "<h3>Search Results</h3><br />\n";
        
    $keyword $_POST["keywords"];
        require_once (
    "../includes/db_connect.php");
        
    $query "SELECT * FROM nfc_entries2 WHERE MATCH(ENTRY) AGAINST ('$keyword')";
        
    $query_result mysql_query($query); 
        if (
    $query_result !== FALSE)
            {
            
    $array_of_months = array (=> "January""February""March""April""May""June""July""August""September""October""November""December");
            
    $flipped_array_of_months array_flip($array_of_months);
            echo 
    "<ol>\n";
            while(
    $row mysql_fetch_array($query_resultMYSQL_BOTH))
                {    
                
    //obtaining a summary
                
    $position_of_keyword strpos($row["ENTRY"], $keyword);
                if (
    $position_of_keyword)
                    {
                    
    $summary substr($row["ENTRY"], $position_of_keyword 50100);
                    
    $summary_tags_stripped strip_tags($summary);
                    
                    
    //highlighting keywords in $summary
                    
    $start_tag "<span style='background-color: yellow'>";
                    
    $end_tag "</span>";
                    
    $highlighted_keyword $start_tag.$keyword.$end_tag;
                    
    $highlighted_summary eregi_replace($keyword$highlighted_keyword$summary_tags_stripped);

                    
    $month array_search($row["MONTH"], $flipped_array_of_months);
                    echo 
    "<li><h4>$month $row[DAY], $row[YEAR]</h4>\n";

                    echo 
    "<p>...$highlighted_summary...</p></li>\n";
                    }
                }
            echo 
    "</ol>";
            
    mysql_free_result($query_result);
            }
        else
            {
            echo 
    "<p>Your search criteria did not match any of the entries. Try again and note that search terms are case sensitive.</p>";
            
    mysql_free_result($query_result);
            }
        }
    ?>
    Thanks.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this:

    if (mysql_num_rows($query) == 0) {
    //no records found
    }

    -Ken

  • #3
    New Coder
    Join Date
    Jun 2004
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks angst. Will try that and see if it works...


  •  

    Posting Permissions

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