WHERE name LIKE %search% not showing data with last name

Discussion in 'Databases' started by leo7068, Apr 6, 2010.

  1. #1
    Here is the problem. I have data in a database like these three lines:

    
    Name                         Ext
    Bob Richards               433
    Bob                           101
    Bob Brown                 244 
    Bob Smith                  528
    
    Code (markup):
    When I use this code:

    <!--Start search code -->
    <?php
    //Search imput value from form	
    $search=STK;
    
    //get the mysql and store them in $sresult
    $sresult = mysql_query("SELECT * FROM BAPhoneList WHERE name LIKE '%$search%'");
    
    //grab all the content and display
    while($row = mysql_fetch_array($sresult)){
    	echo "Name=" .$row['name']. " Ext=". $row['extNumber'];
    	echo "<br />";
    }
    ?>
    <!--end search code -->
    Code (markup):
    My results are only showing the Bob with no last name, like:

    Name=Bob Ext=101
    Code (markup):
    I am missing the other 3 Bobs that should be showing also. I used the % wild card, but no help. Please let me know.
     
    leo7068, Apr 6, 2010 IP
  2. hotfaps

    hotfaps Active Member

    Messages:
    94
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #2
    Excuse what I said first time, Is there a reason you can't do 2 Fields for First name and Last name?

    Not sure if its the space throwing it off or what...



    Try

    $sresult = mysql_query("SELECT * FROM BAPhoneList WHERE name LIKE '% $search %'");

    But with this is will have to have spaces there, if you wanted it to pull like bobbie, I think it will no longer work.
    Test that for me please.


    $sresult = mysql_query("SELECT * FROM BAPhoneList WHERE name LIKE '% $search %' OR name LIKE '%$search %' OR name LIKE '%$search%'");

    Last one is just a guess so it will grab everything like
    Bobby
    Bob Stevens
    Steve Bob
     
    Last edited: Apr 8, 2010
    hotfaps, Apr 8, 2010 IP
  3. NemoPlus

    NemoPlus Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello Leo,

    Personally, I would try to echo $query to be sure that it is correctly filled with $search. Then you can execute the query in your SQL client and see if you find back all Bobs.

    My guess is:

    <!--Start search code -->
    <?php
    //Search imput value from form	
    $search=STK;
    
    // create the query and echo for debugging reasons (temp.)
    // so be sure that php doesn't mess things up, I don't put the variable inside the query directly.
    $query = "SELECT * FROM BAPhoneList WHERE name LIKE '%" . $search . "%'";
    echo $query . "<br />\n";
    
    //get the mysql and store them in $sresult
    $sresult = mysql_query($query);
    
    //grab all the content and display
    while($row = mysql_fetch_array($sresult)){
    	echo "Name=" .$row['name']. " Ext=". $row['extNumber'];
    	echo "<br />";
    }
    ?>
    <!--end search code -->
    Code (markup):
    Good luck and please let us know if it works :)
     
    NemoPlus, Apr 9, 2010 IP