Stuck BYO Database driven WS using php & mysql Chapter 7

Hi all,
I have made it about halfway through Build your own database driven website using php and mysql by Kevin Yank, and am stuck. Basically everything works, except for I cant seem to get it to pull out the jokes using the code provided. I have copied from the book, and downloaded the sorce code from this site, but when I run the code it gives me the error that it failed to fetch the jokes.

Below is the code, it is found on page 223 of the book. In the code, the word category is spelled catagory, it is a typo, but once it was realized, it was easier to keep it, than to change all the instances that had that word, so that is not the problem. Any help would be greatly appreciated.


//Below is the beginning of the search process

if(isset($_GET['action']) and $_GET['action']=='search')
{
include $_SERVER['DOCUMENT_ROOT'].'/includes/db.inc.php';

 
 //The basic SELECT statement

  $select='SELECT id,joketext';
  $from='FROM joke';
  $where='WHERE TRUE';  


 //Specifications for author's search 

   $authorid = mysqli_real_escape_string($link,$_GET['author']);
  if($authorid !='') //An author is selected
   {
   $where .="AND authorid='$authorid'";
   echo "an author has been selected";
   }

//Specifications for Catagory search    

     $catagoryid = mysqli_real_escape_string($link,$_GET['catagory']); 
    if($catagoryid !='')//A catagory is selected
    {
    $from .='INNER JOIN jokecatagory ON id=jokeid';
    $where .='AND catagoryid="$catagoryid"';
    echo "a catagory has been selected";//test to see if working this far
    }

//This is part of the text search

    $text=mysqli_real_escape_string($link,$_GET['text']);
    if($text!='') //Some search text was specified  
    {
    $where .="AND joketext LIKE '%$text%'";
    echo "some text was specified";// test to see if working this far
    }

//pulling out the requested serch criteria
//Here is where I am having the problem.  If I replace the below code
//with a regular select statement it works great, but doesn't seem to 
// like this code too much, can anyone help me spot the problem?

$resultset = mysqli_query($link, $select . $from . $where);

if(!$resultset)
   {
   echo 'Error fetching jokes';  //This is the error message I am getting
   exit();
   }

  while($row = mysqli_fetch_array($resultset))
  {
  $jokes[]=array('id'=>$row['id'], 'text'=>$row['joketext']);
  }
  
include 'jokes.html.php';
exit();
}


// Display search form

include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
$resultset = mysqli_query($link, 'SELECT id, name FROM author');
if (!$resultset)
{
echo "Error fetching authors from database!";
exit();
}

while ($row = mysqli_fetch_array($resultset))
{
$authors[] = array('id' => $row['id'], 'name' => $row['name']);
}

$resultset = mysqli_query($link, 'SELECT id, name FROM catagory');
if (!$resultset)
{
echo "Error fetching catagories from databases!";
exit();
}

while($row = mysqli_fetch_array($resultset))
{
	$catagories[] = array('id' => $row['id'], 'name' => $row['name']);
}

include 'searchform.html.php';
?>

Thanks AL

Change the $where variable to $where = ‘’;

There’s no reason to put ‘WHERE true’ in a query whatsoever.

“whatsoever” is pretty absolute, and i can think of at least one reason to use WHERE TRUE, or at least its more robust cousin WHERE 1=1

using this makes for much simpler application logic to build a dynamic query from a series of conditions, each of which which may or may not exist

without this trick, you would need extra code on each field to determine if it was the first one encountered

if it’s the first one, you need to say WHERE condition, but if it isn’t the first one, the you have to use AND condition, and you have to have all this extra logic on each field

with WHERE 1=1, any condition that you need to include in the query can be added using AND, since the WHERE already exists

does that make sense? did i explain that okay?