Hello,

I got my search engine working and it does show the number of results including the number of pages it should have. But I'm having trouble outputting the results when I click next. When I click the next page it doesn't show any results.

<?PHP
global $search_term;
global $location_term;
global $results;
function getmicrotime()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
$connection_string = "connectionstring.php";
require_once($connection_string);
mysql_select_db("chef") or die ( 'Unable to select database.' );
$RESULTS_LIMIT=10;
if(isset($_GET['search_term']) && isset($_GET['location_term']) && isset($_GET['search_button']))
{
      $search_term = $_GET['search_term'];
	  $location_term = $_GET['location_term'];
	  
    if(!isset($first_pos))
    {
        $first_pos = "0";
    }

    $start_search = getmicrotime();
    $sql_query = mysql_query("SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term')");

    if($results = mysql_num_rows($sql_query) != 0)
            {
                $sql =  "SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term') LIMIT $first_pos, $RESULTS_LIMIT";
                  $sql_result_query = mysql_query($sql);   
            }
    else
            {
                  $sql = "SELECT * FROM searchengine WHERE (product LIKE '%".mysql_real_escape_string($search_term)."%' AND location LIKE '%".mysql_real_escape_string($location_term)."%') ";
                  $sql_query = mysql_query($sql);
                  $results = mysql_num_rows($sql_query);
                  $sql_result_query = mysql_query("SELECT * FROM searchengine WHERE (product LIKE '%".$search_term."%' AND location LIKE '%".$location_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
            }
    $stop_search = getmicrotime();
    $time_search = ($stop_search - $start_search);
}
?>
<?PHP
global $first_pos;
global $sites;
if($first_pos > 0)
{
  $back=$first_pos-$RESULTS_LIMIT;
  if($back < 0)
  {
    $back = 0;
  }
  echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$back' ></a>";
}
if($results>$RESULTS_LIMIT)
{
  $sites=intval($results/$RESULTS_LIMIT);
  if($results%$RESULTS_LIMIT)
  {
    $sites++;
  }
}
for ($i=1;$i<=$sites;$i++)
{
  $fwd=($i-1)*$RESULTS_LIMIT;
  if($fwd == $first_pos)
  {
      echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '><b>$i</b></a> | ";
  }
  else
  {
      echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd '>$i</a> | ";   
  }
}
if(isset($first_pos) && $first_pos < $results-$RESULTS_LIMIT)
{
  $fwd=$first_pos+$RESULTS_LIMIT;
  echo "<a href='index2.php?search_term=".stripslashes($search_term)."&first_pos=$fwd ' > >></a>";
  $fwd=$results-$RESULTS_LIMIT;
}
?>

Can anyone help me out on this?

Also, I think my search doesn't get passed to the next page. Any idea how to preserve that?

Thanks

Dani AI

Generated

Quick summary and root causes (so future readers land here and fix it quickly):

  • The page-processing block was only run when the original form submission flag existed, so clicking plain pagination links skipped the search logic and returned nothing. Relying on the button name is brittle — links must include whatever parameters your code checks for, or your code should check for the actual search terms instead.
  • There was a subtle bug when you set the results count: operator precedence can make mysql_num_rows(...) != 0 evaluate to a boolean and then assign that boolean to $results instead of the row count. That breaks pagination math.
  • Mismatched parameter names (for example firstpos vs first_pos) or accessing $_GET without isset() produces undefined-index notices and wrong offsets.

Practical fixes (safe, minimal changes)

  • Always read and normalize GET values at the top of the page and cast numeric offsets to int:
    $search_term   = isset($_GET['search_term'])   ? trim($_GET['search_term'])   : '';
    $location_term = isset($_GET['location_term']) ? trim($_GET['location_term']) : '';
    $first_pos     = isset($_GET['first_pos'])     ? (int) $_GET['first_pos']     : 0;
  • Get an integer result count explicitly:
    $results = mysql_num_rows($count_query_result); // assign the count, then compare
    if ($results > 0) { /* fetch LIMIT $first_pos,$RESULTS_LIMIT */ }
  • Build pagination links so every needed parameter is preserved (use http_build_query() to avoid forgetting something).

Extra recommendations

  • Move to prepared statements (mysqli or PDO) to avoid SQL injection. Prefer re-querying for each page (cheap for indexed searches) instead of storing big result sets in session. For SEO, use a page= style param, include rel="prev/next" and canonical rules to avoid duplicate-index problems.

Notes on the thread: pointed in the right direction about reading GET and re-querying; found the symptom (missing flag in the link). The robust approach is to stop depending on the submit-button flag and instead rely on explicit, consistently named query parameters.

Recommended Answers

All 11 Replies

any suggestions?

You should read firstpos from $_GET; at the start of the page.

If you want to keep your results without querying, then you need to add them to a $_SESSION variable. Personally I think it is better to re-query the database.

I can see in my URL that it does have the search term for the next page. Can you help me out as to where I should put the $_GET? Sorry I"m a newbie at php.

Thanks

Before line 19 in the first, before 4 in the second:

$firstpos = $_GET['firstpos'];

I put that code in the lines you mentioned but now I get this error

"Undefined index: first_pos in C:\wamp\www\nootri\includes\form.php on line 19"

And the second page still doesn't have a result.

Thanks for the help!

Probably becuase the second issue from my first post is still open: If you want to keep your results without querying, then you need to add them to a $_SESSION variable. Personally I think it is better to re-query the database.

If you think that its better to re-query the database, I would take your word for it since I'm a beginner at this. I'm not sure which way is better. But my problem is that I'm not sure how to code the script I have to make it work. I've been using example tutorial scripts and just modified it to my database.

Can you help me with what I need to do?

Thanks

You can search this forum for even more examples.

I second that, this is kind of FAQ, and a lot of practical examples.
Just pick one and post any problem you get (There is nice example from PHPMyCoder)

I know. I have used an example. Thats why I'm posting here because I have a problem to my script.

I still don't know why my search query is not getting passed to the next page. I have the search term in my URL but it just shows an empty result.

Well I just figured it out.

I had to add search_button=Search in my link

echo "<a href='index2.php?search_term=".stripslashes($search_term)."&search_button=Search&first_pos=$back' >

Thanks for helping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.