Jump to content

problem with search


Gruzin

Recommended Posts

hi everybody,
I'am trying to make a simple search, here is the code but it doesn't work... hope someone can help :(

[color=red]<?php

$var = $_GET['form'] ; // get the query for the search engine
$trimmed = trim($var); //trim whitespace from the stored variable

$con = mysql_connect("localhost","3d","pass");
if(!$con){
  die('Error:'.mysql_error());
}
$selectdb = mysql_select_db("3d",$con);
if(!$selectdb){
  die('Error:'.mysql_error());
  }
$query = "SELECT * FROM $test WHERE test LIKE \"%$trimmed%\" ORDER BY test";
$result = mysql_query($query);
if($result == $trimmed){
  echo $result;
  }
  else{
  echo "no matches found";
  }
mysql_close($con);
?>[/color]
Link to comment
Share on other sites

[code]
<?php

$var = $_GET['form'] ; // get the query for the search engine
$trimmed = trim($var); //trim whitespace from the stored variable

$con = mysql_connect("localhost","3d","pass");
if(!$con){
  die('Error:'.mysql_error());
}
$selectdb = mysql_select_db("3d",$con);
if(!$selectdb){
  die('Error:'.mysql_error());
  }
$query = "SELECT * FROM tablename WHERE test LIKE '%$trimmed%' ORDER BY test";//as said, you had the table as $test which wasn't defined
$result = mysql_query($query);
$num =mysql_num_rows($result);//number of matching rows
if($num > 0){//at least one match
  while($row =mysql_fetch_assoc($result){//to loop through all of the matches
echo $row[fieldname];
echo '<br />';
}
  }
  else{
  echo "no matches found";
  }
mysql_close($con);
?>
[/code]

You cant just echo the result of the query ($result = mysql_query()) because that gives a resource id of the query.

Link to comment
Share on other sites

[code]
<?php

$var = $_GET['form'] ; // get the query for the search engine
$trimmed = trim($var); //trim whitespace from the stored variable

$con = mysql_connect("localhost","3d","pass");
if(!$con){
  die('Error:'.mysql_error());
}
$selectdb = mysql_select_db("3d",$con);
if(!$selectdb){
  die('Error:'.mysql_error());
  }
$query = "SELECT * FROM test WHERE test LIKE '%$trimmed%' ORDER BY test"; //as said, you had the table as $test which wasn't defined
$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);//number of matching rows
if($num > 0){//at least one match
  while($row = mysql_fetch_assoc($result)){ //to loop through all of the matches
echo $row[test];
echo '<br />';
}
  }
  else{
  echo "no matches found";
  }
mysql_close($con);
?>[/code]
Link to comment
Share on other sites

Oh i think i see what you are asking? If you leave it blank then it still displays results? This is due to the search being this:
LIKE '%$trimmed%'

This searches for the $trimmed variables with any amount of any character before and after it. So, if you leave it blank, it will return all the results. You have two choices here really; do you need to use like? Or are you only expecting to get one result back? If so, change it to:
$query = "SELECT * FROM test WHERE test= '$trimmed' ORDER BY test";

If you do need to use like, just add a simple test before hand:
[code]
<?php
if(empty($trimmed)){
echo 'You did not provide a search term';
exit;
}
?>
[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.