Jump to content


Photo

problem with search


  • Please log in to reply
13 replies to this topic

#1 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 01:14 PM

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

<?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);
?>

I don't need your script, I'll try to write it myself

#2 logu

logu
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts

Posted 17 August 2006 - 01:30 PM


hi

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



#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 01:34 PM

Can we have a bit more info? What happens when you run this script? And what is $var ?

#4 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 01:53 PM

well $var meant to be the variable wich stores the info inputed from form. and the result of my script is this: no matches found
I don't need your script, I'll try to write it myself

#5 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 01:59 PM

<?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);
?>

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



#6 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 02:20 PM

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


what does this mean?
I don't need your script, I'll try to write it myself

#7 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:21 PM

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.

#8 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 02:25 PM

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:)
I don't need your script, I'll try to write it myself

#9 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:30 PM

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.

#10 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 02:33 PM

<?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);
?>

I don't need your script, I'll try to write it myself

#11 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:41 PM

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:
<?php
if(empty($trimmed)){
echo 'You did not provide a search term';
exit;
}
?>


#12 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 02:47 PM

GingerRobot thank u very much, I've been trying to do this for along time, How can I thank u?  :)
I don't need your script, I'll try to write it myself

#13 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 17 August 2006 - 02:53 PM

No problem :D

#14 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 17 August 2006 - 02:55 PM

ok thanks again, I've added u to my buddy list ;)
I don't need your script, I'll try to write it myself




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users