In my site I have a search function but have hit on a problem that I can only partly solve.

A user makes a search - one or more results are returned. A user can click on an entry and view additional resources on a new page (word2.php) (that's the idea anyway!).

This is the search code;

// Set up our error check and result check array
$error = array();
$results = array();

// First check if a form was submitted. 
// Since this is a search we will use $_GET
if (isset($_GET['search'])) {
   $searchTerms = trim($_GET['search']);
   $searchTerms = strip_tags($searchTerms); // remove any html/javascript.
   if (strlen($searchTerms) < 1) {
      $error[] = "Search terms must be longer than 1 character.";
   }else {
      $searchTermDB = mysql_real_escape_string($searchTerms); // prevent sql injection.
   // If there are no errors, lets get the search going.
   if (count($error) < 1) {
      $searchSQL = "SELECT * FROM pro_words WHERE";
      // grab the search types.
      $types = array();
      if (count($types) < 1)
         $types[] = "`word` LIKE '%{$searchTermDB}%'"; 
          $andOr = isset($_GET['matchall'])?'AND':'OR';
      $searchSQL .= implode(" {$andOr} ", $types) . " ORDER BY `word`"; // order by word.

      $searchResult = mysql_query($searchSQL) or trigger_error("There was an error.<br/>" . mysql_error() . "<br />SQL Was: {$searchSQL}");
      if (mysql_num_rows($searchResult) < 1) {
         $error[] = "Your search yielded no results. Sorry.";
      }else {
         $results = array(); // the result array
         $i = 0;
         while ($row = mysql_fetch_assoc($searchResult)) {
            $results[] = "{$row['word']}<br />";
            echo '<h2><tr><td align="left"><a href="word2.php?w=' . $row['word'] . '">' . $row['word'] . '</a></td></tr></h2>';

function removeEmpty($var) {
   return (!empty($var)); 

You'll see that the code links to file word2.php and that's where the problem lies. I want to display additional columns from the table on word2.php.

This is the business part from word2.php;

$query = "SELECT * FROM words";  

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result)){

echo $row['sentence1'];


This returns all entries in the column sentence1.

I think it comes down to how the variables are defined (and where they are defined) but I don't know how to handle that - is there a way of splittig $result for example that would help? Could I use $_POST or $_GET?

I have had partial success in resolving the problem but it's still not working as it should, so that's still a problem.

Thanks in advance for any help - it'd be very much appreciated; please say if anything's unclear.

There are two main ways that you can pass variables from one module to another. Using a form will pass the variables entered by the user to the module that is identified as the 'action' for the form (as Get or Post variables). The more general purpose way is to use Session variables. These are available to every module as long as the Session is alive (typically until the browser is closed at the user end).

Thanks for the reply.

$_SESSION variables look like the way to go - almost there in fact.

There#s just one problem - I defined the variable like this in search.php

$_SESSION[$sentence1] = $row['sentence1'] ;

and echo it out in word2.php like this

echo $_SESSION[$sentence1] ;

It repeats x number of times, where x is the number of entries in the table.

Could you tell me how to limit that to one instance please?


You need to be more specific about what you mean by "repeat" and what it is you want. If you are using the code that you showed previously for word2.php, then it will echo every entry in the table because that is what you told it to do. If you want to select only one row of the table, then you need to add a Where clause to your Select. If you want to read all of the records and do the selection in the code, then you need an IF statement before the echo to choose the one you want.

Thanks again, your help's much appreciated.

This is what I want my search/display files to do;

- user makes a search of a table on a database
- one or more entries are returned
- the results link to a file word2.php
- if the user searches for the word 'example' s/he clicks on that result to be taken to word2.php. The table contains columns for the word ('word'), the type of word ('type') and a sentence showing its use ('sentence'). I would like word2.php to contain column entries for 'word' 'type' and 'sentence' that relate to the choice made by the user.

Hope that's clearer now.

From what you've said, I think SELECT in word2.php will need a WHERE clause.

Thanks again for your help.