-6

1.This is my code search WHERE 1";

    foreach ($terms as $each){
    $i++;

    if ($i == 0)
        $query .= "keywords LIKE '%$each%' "; 
    else
        $query .= "OR keywords LIKE '%$each%' "; 

    }

    //connect
    Mysql_connect ("localhost", "root","");
    Mysql_select_db("search");

    $query = mysql_query($query);
    $numrows = mysql_num_rows($query);
    if ($numrows > 0) {

        While ($row = mysql_fetch_assoc($query))  {
            $id = $row ['id'];
            $tittle = $row ['tittle'];
            $description = $row ['description'];
            $keywords = $row ['keywords'];
            $link = $row ['link'];

            echo "<h2><a href='$link'>$tittle</a></h2>
            $description<br /><br />";
    //disconnect
    mysql_close();

        }   

    }

    Else
        echo "No results found for \"<b>$k</b>\""

?

1 Answers1

0

Although your question is poorly written and there's missing information the problem could be that $i will never be 0 if you have $i++ before the if statement.

Try to put the $i increment after the if statement.

foreach ($terms as $each){
    if ($i == 0){
        $query .= "keywords LIKE '%$each%' "; 
    }else{
        $query .= "OR keywords LIKE '%$each%' "; 
    }
    $i++
}
gbestard
  • 1,177
  • 13
  • 28