...

View Full Version : problem with for... else... statement for searching database



mat106
09-12-2005, 05:27 PM
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
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
if($query_result) but that didn't work either.

Can anyone see what the problem is:

<?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 (1 => "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_result, MYSQL_BOTH))
{
//obtaining a summary
$position_of_keyword = strpos($row["ENTRY"], $keyword);
if ($position_of_keyword)
{
$summary = substr($row["ENTRY"], $position_of_keyword - 50, 100);
$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.

angst
09-12-2005, 05:40 PM
try this:

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

-Ken

mat106
09-12-2005, 05:47 PM
Thanks angst. Will try that and see if it works...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum