LIKE not working

Discussion in 'MySQL' started by cro91, Oct 20, 2011.

  1. #1
    I am trying to query my database and find only the titles that start with the letter B and I'm using this php script.

    
    <?php
    ini_set ("display_errors", "1"); 
    error_reporting(E_ALL);
        $query = mysql_query("SELECT Anime_Name FROM Anime WHERE Anime_Name LIKE 'B%'");
        $num_rows = mysql_num_rows($query);
        if($num_rows > 1) 
            {
                while ($row = mysql_fetch_assoc($query))
                { 
                    echo $row['Anime_Name'].'<br />'; 
                } 
            }
        else if($num_rows < 1)
        {
            echo "No Results";
        }                            
    ?>
    
    
    
    Code (markup):
    And its returning nothing. I know there is a B in the column Anime_Name because I went and made one specifically named "B" just to test it.

    I also have the exact same script with SELECT Anime_Name FROM Anime WHERE Anime_Name LIKE 'A%' and it works fine. The only dif is its in its own <?php tags about 3 lines up. If I change the 'B%' to 'A%' it works fine.

    Is there something with LIKE that I am missing? Searching for any syntax examples on w3schools or tizag brings up nothing and they act like the command is not even there. Any help?
     
    cro91, Oct 20, 2011 IP
  2. faqu1337

    faqu1337 Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Hey,

    You said you have ONE B in the column. So I think you should replace this "if($num_rows > 1)" with this "if($num_rows >= 1)".
     
    faqu1337, Oct 21, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    Or

    if($num_rows)

    if you don't want unneeded code.
     
    Rukbat, Oct 21, 2011 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Make that

    if(!$num_rows) echo "No Results";

    (Cataracts - I didn't notice that I left out the bang.)
     
    Rukbat, Oct 24, 2011 IP