Gruzin Posted August 17, 2006 Share Posted August 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
logu Posted August 17, 2006 Share Posted August 17, 2006 hi i think you r missing the value for $test in the query. r u assigning it any where Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 Can we have a bit more info? What happens when you run this script? And what is $var ? Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 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] Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 [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 rowsif($num > 0){//at least one match while($row =mysql_fetch_assoc($result){//to loop through all of the matchesecho $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. Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 [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 16no matches found[/color]what does this mean? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 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:) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 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. Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 [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 rowsif($num > 0){//at least one match while($row = mysql_fetch_assoc($result)){ //to loop through all of the matchesecho $row[test];echo '<br />';} } else{ echo "no matches found"; }mysql_close($con);?>[/code] Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 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]<?phpif(empty($trimmed)){echo 'You did not provide a search term';exit;}?>[/code] Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 GingerRobot thank u very much, I've been trying to do this for along time, How can I thank u? :) Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted August 17, 2006 Share Posted August 17, 2006 No problem :D Quote Link to comment Share on other sites More sharing options...
Gruzin Posted August 17, 2006 Author Share Posted August 17, 2006 ok thanks again, I've added u to my buddy list ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.