I have made a searching script that returns results but something is not quite right I cant get the paging to work... I Think it has something to do with the SQL query but Im not sure maybe you guys know

I need a line that figures out the total number of line selected in DB conducted in the search.

Also Im not sure about line 141 either...
$total_pages = ......

Also an unrelataed problem:
If you conduct a search using this script, containing say three words, it will return results for all three but only if the three are found, but if 2 are found and not the last one it returns: No mathes ??

Here goes the code anyway:

//This script assumes you are searching 3 fields
$hostname_logon = "localhost" ;  
$database_logon = "test" ; 
$username_logon = "root" ; 
$password_logon = "" ;  
//open database connection
 $connections = mysql_connect($hostname_logon, $username_logon, $password_logon) or die ( "Unabale to connect to the database" );
 //select database
 mysql_select_db($database_logon) or die ( "Unable to select database!" );

// If current page number, use it
// if not, set one!

    $page = 1;
} else {
    $page = $_GET['page'];

// Get the search variable from URL
  $var = @$_GET['q'] ;
//trim whitespace from the stored variable
  $trimmed = trim($var);
//separate key-phrases into keywords
  $trimmed_array = explode(" ",$trimmed);

// check for an empty string and display a message.
if ($trimmed == ""){
  $resultmsg =  "<p>Search Error</p><p>Please enter a search...</p>" ;
// Search must be at least 3 characters long
if (strlen($trimmed)<3){
    $resultmsg =  "<p>Search Error</p><p>You must search for at least 3 characters</p>" ;

// check for a search parameter
if (!isset($var)){
  $resultmsg =  "<p>Search Error</p><p>We don't seem to have a search
parameter! </p>" ;
// Build SQL Query for each keyword entered
foreach ($trimmed_array as $trimm){
      // EDIT HERE and specify your table and field names for the SQL query
     $query = "SELECT * FROM annonsmarknad WHERE rubrik LIKE '%$trimm%' OR pris like '%$trimm%' OR fritext like '%$trimm%' ORDER BY rubrik DESC" ;
     // Execute the query to  get number of rows that contain search kewords
     $numresults=mysql_query ($query);
     $row_num_links_main =mysql_num_rows ($numresults);
    $row= mysql_fetch_array ($numresults);

      //store record id of every item that contains the keyword in the array we need to do this to avoid display of duplicate searchresult.
          $adid_array[] = $row[ 'annonsid' ];
      }while( $row= mysql_fetch_array($numresults));
} //end foreach

if($row_num_links_main == 0 && $row_set_num == 0){
   $resultmsg = "<p>Search results for: ". $trimmed."</p><p>Sorry, your
search returned zero results</p>" ;
   //delete duplicate record id's from the array. To do this we will use array_unique function
   $tmparr = array_unique($adid_array);
   foreach ($tmparr as $v) {
       $newarr[$i] = $v;

// now you can display the results returned. But first we will display the search form on the top of the page

<form action="sok2.php" method="get" name="search">
  <div align="center">
      <input name="q" type="text" value="<?php echo $q; ?>" size="15">
      <input name="search" type="submit" value="Search">

// display what the person searched for.
if( isset ($resultmsg)){
  echo $resultmsg;
  echo "Search results for: " . $var;

foreach($newarr as $value){
// EDIT HERE and specify your table and field names for the SQL query
$query_value = "SELECT * FROM annonsmarknad WHERE annonsid=".$value."";
$num_value=mysql_query ($query_value);
$row_linkcat= mysql_fetch_array ($num_value);
$row_num_links= mysql_num_rows ($num_value);

//now let's make the keywods bold. To do that we will use preg_replace function.
//Replace field
  $titlehigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat['rubrik' ] );
  $linkhigh = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat['pris' ] );
  $linkdesc = preg_replace ( "'($var)'si" , "<b>\\1</b>" , $row_linkcat['fritext' ] );

foreach($trimmed_array as $trimm){
    if($trimm != 'b' ){
        $titlehigh = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" ,
        $linkhigh = preg_replace( "'($trimm)'si" , "<b>\\1</b>" ,
        $linkdesc = preg_replace( "'($trimm)'si" ,  "<b>\\1</b>" ,
//end highlight
  } //end foreach $trimmed_array
<?php echo $titlehigh; ?><br>
<?php echo $linkhigh; ?><br>
<?php echo $linkdesc; ?><br>
<?php echo $max_results; ?><br>
<?php echo $from; ?><br>

// ------------- Paging --------------------

// Define the number of results per page
$max_results = 2;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);

$query_value .=" LIMIT $from, $max_results";

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($row_num_links_main / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
echo "</center>";