-1

I have been following this tutorial to make a search engine. I am getting an error(check on the website, LINK. Try to search and you get an error)

Code:

    $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's>$title</a></h2>
                    $description<br /><br />";
                }

            }else
                echo "No Results found for <b>$k</b>";

            //disconnect
            mysql_close();

        ?>

http://www.youtube.com/watch?v=OPmBJhhuwSM

user2708570
  • 79
  • 1
  • 8
  • try: $query = mysql_query($query) || die(mysql_error()); what does it output ? – Christoph Diegelmann Aug 22 '13 at 18:48
  • 1
    There's approximately $INFINITY duplicates of this exact same error message on the site. They all boil down to the same thing: Your DB operation has failed somehow. Your code simply assumes nothing could EVER fail. And now you're stuck with error messages because you have no error handling at all. – Marc B Aug 22 '13 at 18:50

1 Answers1

0

mysql_query() returns FALSE on error, so you need to check if "$query" is FALSE and handle the error instead of passing it through to mysql_num_rows().

if(!$query) {
  echo mysql_error();
} else {
  $numrows = mysql_num_rows($query);
  ...
}
  • That didnt help. Now it says no database selected – user2708570 Aug 22 '13 at 19:25
  • @user2708570: how much hand holding do you need? An error that says "no database selected" probably means *you have no database selected*. – DCoder Aug 22 '13 at 19:37
  • I selected a database above this code. – user2708570 Aug 22 '13 at 19:55
  • @user2708570: If you are seeing an error message from mysql_error() then something is obviously wrong. The whole point of that code snippet I posted was to do some kind of error checking to see why your query is failing. How does that not help? – James Tracy Aug 27 '13 at 18:28