webmasters
webmasters

Reputation: 5831

Mysql query error, not equal to

If title is: $title = wp_title('',0); - query does not exclude: AND posttitle <> '$title'" If title is for example: $title = 'test is the best'; - query does exclude AND posttitle <> ''test is the best'"

 $query = mysql_query("SELECT posttitle, posturl, siteurl, MATCH (posttitle,posturl,siteurl) AGAINST ('$title') AS score FROM interlinks WHERE MATCH (posttitle,posturl,siteurl) AGAINST ('$title') AND `posttitle` <> '$title'");

Upvotes: 1

Views: 1567

Answers (2)

Ben Lee
Ben Lee

Reputation: 53319

Generally, string comparisons in mysql are not case sensitive because the tables by default use latin1 character set and latin1_swedish_ci collation. To get them to compare in a case sensitive way, use the BINARY operand like this:

WHERE MATCH (posttitle,posturl,siteurl) AGAINST ('$title') 
AND BINARY posttitle != '$lowercasetitle'

Upvotes: 0

OMG Ponies
OMG Ponies

Reputation: 332581

You have a typo - posttitle doesn't need single quotes, use:

SELECT posttitle,  
       posturl, 
       siteurl, 
       MATCH (posttitle,posturl,siteurl) AGAINST ('$title') AS score 
  FROM interlinks 
 WHERE MATCH (posttitle,posturl,siteurl) AGAINST ('$title') 
   AND posttitle <> '$lowercasetitle'

Putting single quotes indicates to SQL that it's dealing with a string, not a column reference. So the comparison to your variable was that it didn't equal the string 'posttitle', rather than the value in the column.

In MySQL, backticks (`) are used for escaping registered keywords:

SELECT posttitle,  
       posturl, 
       siteurl, 
       MATCH (posttitle,posturl,siteurl) AGAINST ('$title') AS score 
  FROM interlinks 
 WHERE MATCH (posttitle,posturl,siteurl) AGAINST ('$title') 
   AND `posttitle` <> '$lowercasetitle'

Upvotes: 4

Related Questions