mysql - My Search engine code in php giving me no result -
i making search engine in php,but receiving no or 0 result. when search keywords in database showed me no result. can please me doing wrong.. below code.
<?php $k = $_get['k']; $terms = explode(" ", $k); $query = ("select * search "); foreach($terms $each){ $i++; if($i == 1) $query.= (" 'keywords' '%$each%' "); else $query.= (" or 'keywords' '%$each%' "); } //connect db require("./db.php"); $query = mysql_query($query); $numrows = mysql_num_rows($query); if( $numrows>0 ){ while( $row = mysql_fetch_assoc($query) ) $id = $row['id']; $title = $row['title']; $description = $row['description']; $keywords = $row['keywords']; $link = $row['link']; echo "<h2><a href = '$link'>$title</a></h2> $description </br> </br>"; } else echo "no results found \"<b>$k</b>\""; //disconnect db mysql_close(); ?>
as mentioned in comment tadman, need escape input (hints, use mysqli functions , use mysqli_real_escape_string, or perhaps pdo).
with said, think need remove quotes keywords column name so:
if($i == 1) $query.= (" keywords '%$each%' "); else $query.= (" or keywords '%$each%' ");
Comments
Post a Comment