Page 1 of 1

Search engine coded, select more then one field?

Posted: Fri Sep 30, 2005 5:06 am
by smudda
hey there,
i've coded this little search script:

Code: Select all

<?php
  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10; 

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>nope</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

// Build SQL Query  
$query = "select * from thing_users where EXAMPLE like \"%$trimmed%\"  
  order by EXAMPLE"; // EDIT HERE and specify your table and field names for the SQL query

 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);

// No result

if ($numrows == 0)
  {
  echo "<h1>Oops!</h1>";

  }

// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }

// get results
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "bla";

// begin to show results set
// echo "<strong>Results</strong><ul>\n";

$count = 1 + $s ;

// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["ort"];
  $benutzer = $row["user"];
  $id = $row["id"];
  $about = $row["about"];
  $age = $row["age"];
  $EXAMPLE = $row["EXAMPLE"];
  $EXAMPLEzwei = $row["EXAMPLEzwei"];
  $EXAMPLEdrei = $row["EXAMPLEdrei"];
  $EXAMPLEvier = $row["EXAMPLEvier"];
  $EXAMPLEfuenf = $row["EXAMPLEfuenf"];
  $EXAMPLEsechs = $row["EXAMPLEsechs"];

  $count++ ;
  }

$currPage = (($s/$limit) + 1);

//break before paging
  echo "</ul>";

?>


now this works, but i want now to make it search through more then one field. for example not only field EXAMPLE i also want EXAMPLEzwei, EXAMPLEdrei, EXAMPLEvier, EXAMPLEfuenf, EXAMPLEsechs in the same table to get searched. how do i do that?

this doesn't work:

Code: Select all

// Build SQL Query  
$query = "select * from thing_users where EXAMPLE, EXAMPLEzwei, EXAMPLEdrei, EXAMPLEvier, EXAMPLEfuenf, EXAMPLEsechs like \"%$trimmed%\"  
  order by EXAMPLE"; // EDIT HERE and specify your table and field names for the SQL query
any hints? :(

Posted: Fri Sep 30, 2005 5:18 am
by n00b Saibot
Try this

Code: Select all

select * from thing_users where 
EXAMPLE like "%$trimmed%" OR
EXAMPLEzwei like "%$trimmed%" OR
EXAMPLEdrei like "%$trimmed%" OR
EXAMPLEvier like "%$trimmed%" OR
EXAMPLEfuenf like "%$trimmed%" OR
EXAMPLEsechs like "%$trimmed%"
  order by EXAMPLE

Posted: Fri Sep 30, 2005 5:49 am
by smudda
n00b Saibot, works awesome!!!

one more question
i have german umlauts in the database, like üäö, when i enter these at the end of a search string, it doesn't bring me any results. any idea of how i can fix that?

Posted: Fri Sep 30, 2005 5:59 am
by n00b Saibot
i can think of two ways - first is to replace them when storing with something that you will replace back with when displaying, and second is that mysql supports character sets like latin1 etc.. so setting that may also help.