Jump to content

Archived

This topic is now archived and is closed to further replies.

Gruzin

problem with search

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]

Share this post


Link to post
Share on other sites

hi

  i think you r missing the value for $test in the query.
  r u assigning it any where

Share this post


Link to post
Share on other sites
Can we have a bit more info? What happens when you run this script? And what is $var ?

Share this post


Link to post
Share on other sites
well $var meant to be the variable wich stores the info inputed from form. and the result of my script is this: [color=red]no matches found[/color]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[color=red]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /users/3d_cn~1/html/test/search.php on line 16
no matches found[/color]

what does this mean?

Share this post


Link to post
Share on other sites
I expect it means there was something wrong with your query. Try this:
$result = mysql_query($query) or die(mysql_error());

This will give more information on any error that could be in the query.

Share this post


Link to post
Share on other sites
I found it, but when I input nothing in the form it still displays the tabel value, how can I prevent that? Thank a lot GingerRobot, basiclly it works:)

Share this post


Link to post
Share on other sites
No problem.

What do you mean by still displays the table value? Can you show the code that you are currently using? And enclose it in [code*][/code*] tags and full php tags(<?php ?>) so we can see it highlighted, makes it a lot easier to look at.

Share this post


Link to post
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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
GingerRobot thank u very much, I've been trying to do this for along time, How can I thank u?  :)

Share this post


Link to post
Share on other sites
ok thanks again, I've added u to my buddy list ;)

Share this post


Link to post
Share on other sites

×

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.