Can't find error in 10 lines of simple code

Discussion in 'PHP' started by zodiakasxxx, Mar 19, 2010.

  1. #1
    <?
    $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"...
     
    zodiakasxxx, Mar 19, 2010 IP
  2. rainborick

    rainborick Well-Known Member

    Messages:
    424
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    120
    #2
    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.
     
    rainborick, Mar 19, 2010 IP
  3. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    you cant use if (mysql_num_rows()) {, because it does not necessarily return TRUE or FALSE.
     
    ThomasTwen, Mar 19, 2010 IP
  4. guardian999

    guardian999 Well-Known Member

    Messages:
    376
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    mysql_num_rows() not return boolean

    you should use
    
    $rows = mysql_num_rows();
    if($rows > 0){ echo "yes";}
    
    Code (markup):
     
    guardian999, Mar 19, 2010 IP
  5. zodiakasxxx

    zodiakasxxx Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yea, thanks guys.
     
    zodiakasxxx, Mar 19, 2010 IP
  6. K1llswitch

    K1llswitch Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    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.
     
    K1llswitch, Mar 20, 2010 IP
  7. mattinblack

    mattinblack Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    "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!
     
    mattinblack, Mar 20, 2010 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    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):
     
    shallowink, Mar 20, 2010 IP
  9. zodiakasxxx

    zodiakasxxx Peon

    Messages:
    128
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    zodiakasxxx, Mar 22, 2010 IP