2

Ok, I have been on this for 1 hour now.. I am missing something very simple ? I need a new set of eyes on this ? I searched here and found a few things and tried to implement , but still came up short .

I have magic quotes turned off:

My search is for Sam's Club

In the database it is entered as : Sam's Club as well

Simple search function:

$q = htmlspecialchars($q);
// changes characters used in html to their equivalents, for example: < to &gt;
$q = mysql_real_escape_string($q);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE()
AND (`Title` LIKE '%".$q."%')") or die(mysql_error());

Still coming up empty result?

Obviously if I search for Sam it will query a result?

Here are two images from my phpMyAdmin:

enter image description here

enter image description here

SOLVED

I found the problem. I was including another list of functions in my header that I forgot about. That was messing with the variable.

Thanks for the troubleshooting! I still learned a ton!!

eberswine
  • 1,224
  • 3
  • 20
  • 39

2 Answers2

3

I would suggest getting rid of htmlspecialchars() it is converting your quote to &apos; or even &#039;.

So when it tries to search the database it is using Sam&apos;s Club or Sam&#039;s Club as your search, but it won't be there because it is saved as: Sam's Club.

$q = mysql_real_escape_string($q);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE()
AND (`Title` LIKE '%".$q."%')") or die(mysql_error());



Please run this and reply with the string that is generated.

$q = mysql_real_escape_string($q);
echo "SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE() AND (`Title` LIKE '%".$q."%')";
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • Ok , got rid of htmlspecialchars(), but still no dice ? Do I need to write a small function to replace characters before running the query ? – eberswine May 02 '13 at 19:23
  • Can you `echo` the query string just before you execute it and paste it here so it's more clear what you're actually executing? – tadman May 02 '13 at 19:24
  • It is echo Sam's Club before and after now, but still no dice. I do have a $q = (isset($_GET['q']) ? $_GET['q'] : ""); before all this code too ? – eberswine May 02 '13 at 19:28
  • 1
    please echo out your full query. `echo "SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE() AND (\`Title\` LIKE '%".$q."%')"` – Get Off My Lawn May 02 '13 at 19:32
  • I did echo $raw_results and came up with this: Resource id #13 – eberswine May 02 '13 at 19:37
  • I added two images from my database – eberswine May 02 '13 at 19:41
  • you're searching for `Sam"s Club` and not `Sam's Club` (note that one is a quote and one is an apostrophe). – Get Off My Lawn May 02 '13 at 19:43
  • Or those could be two apostrophes. – Marcel Korpel May 02 '13 at 19:45
  • That is the strange thing in phpMyAdmin , I search for it with a single quote and then it returns the results page and it turns the ' into '' ? ( see above in purple and green image ) – eberswine May 02 '13 at 19:48
  • When I meant echo out the query, I didn't mean the result of mysql_query. Please look at my update. – Get Off My Lawn May 02 '13 at 19:56
  • SQL STRING=*SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE() AND Title LIKE '%Sam's%'* – eberswine May 02 '13 at 19:59
1

Try this code:

   echo 'value q before=*' . $q . '*';

   $q = mysql_real_escape_string($q);
   echo 'value q after=*' . $q . '*';
   $q2 = mysql_escape_string($q);
   echo 'value q2 after=*' . $q2 . '*';

    // makes sure nobody uses SQL injection

    $sql = "SELECT * FROM this, this2 WHERE this.TypeID = this2.TypeID AND this.status = 'Active' AND this.endDate >= CURDATE() AND `Title` LIKE '%".$q."%'";
    echo 'SQL STRING=*'.$sql.'*';
    $raw_results = mysql_query($sql) or die(mysql_error());
    echo 'RAW RESULTS=' . print_r($raw_results, true);
bestprogrammerintheworld
  • 5,417
  • 7
  • 43
  • 72