<? $seo_word = $_GET['word']; $word_safe = mysql_real_escape_string($seo_word); if(mysql_num_rows(mysql_query("SELECT * FROM enlt WHERE word ='$word_safe' "))){ echo "yes"; } else{echo "no";}?> It's always returning "no", when it should find existing line and return "yes"...
The first step to take is to unwind your 'if' statement and display the query string and the result of your mysql_query() call. That should tell you what your code is doing and why it fails.
mysql_num_rows() not return boolean you should use $rows = mysql_num_rows(); if($rows > 0){ echo "yes";} Code (markup):
Also many mistakes result in a mysql error, so always put or die(mysql_error()) after your mysql_query() that way if there is an error, it kills the script and tells you what was wrong.
"SELECT * FROM enlt WHERE word ='$word_safe' " This will not work at all because PHP does not expand terms in single quotes. It is searching for word to be literally equal to '$word_safe' and not the value of $word_safe. You need: "SELECT * FROM enlt WHERE word =\"$word_safe\"" for it to work!! The original would produce no mysql error because its a perfectly valid request - just one that will never result in a match unless you add a word value of '$word_safe' into your database!
ok, you're wrong. That is a perfectly valid method. While your facts are correct, what you are skipping is the fact the statement is already inside of double quotes thus the single quote is just another character. <?php $var = "bar"; echo "SELECT * FROM table WHERE this='$var' "; ?> Code (markup): output : SELECT * FROM table WHERE this='bar' Code (markup):
Thanks guys for help. The error wasn't in php at all. The problem was the space after the word in DB. All I needed was to strip spaces.