944,155 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 610
  • PHP RSS
Oct 26th, 2009
0

Wild Card search not working...

Expand Post »
Hey, I need to create a search engine that searchs through a database but the wild card doesn't work for me:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. include ('header.php');
  3. $s = $_POST['search_term'];
  4. $by = $_POST['by'];
  5.  
  6. if(!$s && $by) // Check that the details are entered correctly
  7. {
  8. echo '<p>You have not entered the required fields</p>';
  9. exit();
  10. }
  11. // LIKE '%$searchterm%'
  12. $query = "SELECT * FROM products WHERE artist LIKE '% $s %'";
  13. $result = mysql_query($query) or trigger_error("Query: $query \n<br />Mysql error: " . mysql_error());
  14.  
  15. if(mysql_affected_rows() >= 1)
  16. {
  17. while($row = mysql_fetch_array($result))
  18. {
  19. echo '<p>Your search result for ' .$search_term. '';
  20. echo '<blockquote>';
  21. echo '<img src="' .$row['pic']. '">';
  22. echo '<p>' .$row['desc']. '<br />';
  23. echo 'Price: ' .$row['price']. '<br />';
  24. echo '</blockquote>';
  25. }
  26. }else{
  27. echo '<p>No artist found, did you spell it right?</p>';
  28. }
  29.  
  30. ?>

Any help would be great. Thanks.
Similar Threads
Reputation Points: 41
Solved Threads: 7
Posting Whiz in Training
Phil++ is offline Offline
233 posts
since Jan 2009
Oct 26th, 2009
1
Re: Wild Card search not working...
Remove the spaces between the %s and the value.

If the user entered 'Google' then the search query would look like:
PHP Syntax (Toggle Plain Text)
  1. $query = "SELECT * FROM products WHERE artist LIKE '% Google %'";
This would return values where the products column had an entry of 'something Google something'
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Oct 27th, 2009
1
Re: Wild Card search not working...
Hey.

Also, on line #15 you use the mysql_affected_rows function, where you should be using the mysql_num_rows function.

The mysql_affected_rows function only returns the number or rows affected by INSERT, UPDATE, REPLACE or DELETE queries. SELECT queries have no affect on any rows, and as such, the function doesn't return a valid result for them.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 27th, 2009
0
Re: Wild Card search not working...
if your query is failing try to echo the query and execute it directly in the mysql and post if any error you getting there. or else post the error you echoing in your php
Reputation Points: 29
Solved Threads: 76
Practically a Master Poster
network18 is offline Offline
616 posts
since Sep 2009
Oct 27th, 2009
0
Re: Wild Card search not working...
Also addslashes(); to your search term somtimes users entering quotes can cause sql to fail.
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009
Oct 27th, 2009
1
Re: Wild Card search not working...
Click to Expand / Collapse  Quote originally posted by liamfriel ...
Also addslashes(); to your search term somtimes users entering quotes can cause sql to fail.
The mysql_real_escape_string function would be better. It does more than just add slashes, it escapes any char that would mess up the query.

It should be used on any data that is to be inserted into a MySQL query, especially user-supplied data. (See SQL Injection)
Last edited by Atli; Oct 27th, 2009 at 1:18 pm.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Oct 28th, 2009
0
Re: Wild Card search not working...
Click to Expand / Collapse  Quote originally posted by Atli ...
The mysql_real_escape_string function would be better. It does more than just add slashes, it escapes any char that would mess up the query.

It should be used on any data that is to be inserted into a MySQL query, especially user-supplied data. (See SQL Injection)
Thanks - I have spent so long using a custom function(that uses mysql_real_escape_string()) to validate my data I forgot about it.

If anyone is interested:

PHP Syntax (Toggle Plain Text)
  1. function make_safe($string) {
  2. $string = preg_replace('#<!\[CDATA\[.*?\]\]>#s', '', $string);
  3. $string = strip_tags($string);
  4. $string = htmlentities($string, ENT_NOQUOTES, 'UTF-8', false);
  5. $string = stripslashes($string);
  6. $string = mysql_real_escape_string($string);
  7. return $string;
  8. }
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009
Oct 28th, 2009
0
Re: Wild Card search not working...
Double post please delete
Last edited by liamfriel; Oct 28th, 2009 at 10:51 am. Reason: double post - please delete
Reputation Points: 13
Solved Threads: 13
Junior Poster
liamfriel is offline Offline
101 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: New to PHP and wanting to go on a course
Next Thread in PHP Forum Timeline: Different function for different buttons in php-mysql question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC