0

I am trying to run a simple PHP search script that will show look through my mySQL database and present the results on a separate page that I've created. When I run the code, my results don't show up. Can someone please help me?? This is the code that i'm currently using:

<?php

    error_reporting(E_A`enter code here`LL);
    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("josetogbecom.fatcowmysql.com","username","password");
        mysql_select_db("gff_hff6a144eg");

        $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 Search Results Were Found for \"<b>$k<b>\"";
        }


        // disconnect
        mysql_close();

    ?>

Thanks in advance!
3
  • 2
    You have a username and password in clear text! Edit it, quick! Commented Apr 24, 2014 at 15:15
  • Thanks, I just changed my password info Commented Apr 24, 2014 at 15:19
  • error_reporting(E_Aenter code hereLL); this is wrong, should be error_reporting(E_ALL);. Commented Apr 24, 2014 at 15:34

2 Answers 2

1

Check if your $i counter var is set to $i=0 before foreach loop

Sign up to request clarification or add additional context in comments.

Comments

1

Bug at this line:

echo "No Search Results Were Found for \"<b>$k<b>/"";

Should be:

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

Bug at this line:

mysql close();

Should be:

mysql_close();

Bug at this line:

$i++;

You should always initialize your variables, like this:

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

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

If the script still doesn't work after correcting these two bugs, add to the beginning of the script these 2 lines and tell us what you see:

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

6 Comments

Thanks! but the search results are still not appearing!
@user3417199 If still nothing than add those last two lines right after <?php and tell us what you see.
this is what I see: 0){ while ($row = mysql_fetch_assoc($query)){ $id = $row['id']; $title = $row['title']; $description = $row['description']; $keywords = $row['keywords']; $link = $row['link']; echo " $title $description "; } } else { echo "No Search Results Were Found for \"$k\""; } // disconnect mysql_close(); ?>
My script worked! I just wanted to thank you all for helping and being patient!
@user3417199 No problem, thats what this site is for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.