0

I'm using PHP to search my database and present information on a separate search results page. Please see the php script below. With my current script I can only showcase one result. How can I showcase multiple results? Also, how can I add a CSS class to a part of my PHP code?

<?php

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

        $k = $_GET['k'];
        $terms = explode(" ", $k);
        $query = "SELECT * FROM search WHERE ";

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

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

        // connect
        mysql_connect("host","user","pass");
        mysql_select_db("DB1");

        $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 "<h1><a href='$link'>$title</a></h1>
                    <h2>$link</h2><br /><br />
                    $description<br /><br />
                    $keywords<br /><br />";
            }   

        }
        else 
        {
            echo "No Search Results Were Found for \"<b>$k<b>\"";
        }


        // disconnect
        mysql_close();

?>

Please help! Thanks in advance.

random21
  • 579
  • 1
  • 5
  • 15
user3417199
  • 19
  • 1
  • 5
  • Read these before you continue: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – Luceos Apr 24 '14 at 18:57
  • debug the $query variable to see what you are querying – rod_torres Apr 24 '14 at 19:07
  • I don't know what you mean by Debug the $query variable. I literally copy pasted this script from somewhere :/ – user3417199 Apr 24 '14 at 19:28
  • try this and check for given error `$query = mysql_query($query) or die(mysql_error());` – Jerko W. Tisler Apr 24 '14 at 19:47
  • Nothing happened when I did either one of the following: $query = mysql_query($query) or die(mysql_error()); – user3417199 Apr 28 '14 at 15:17

0 Answers0