Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    138
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Having some trouble with this query...

    Need a little noob help here.. I'm running this query which should search my MySQL DB and return only the records that have the same last name and a first name that begins with the first initial that the person enters. It works fine to look up the last name, but when I try to add on the first initial, it craps out. I'm having a problem with the proper wildcards, I believe, but maybe my PHP is flawed as well.. Take a look if you would...

    PHP Code:
    <?
      
    if($_POST['submit'])
      {     
        
    mysql_connect("localhost","orlandoi_referra","referral2"); 
        
    mysql_select_db("orlandoi_referralowners") or die ('I cannot connect to the database because: ' mysql_error()); 

     
    $LstName make_safe($_POST['LstName']);
    $FirstInitial make_safe($_POST['FirstInitial']);


     

        
    $result mysql_query("SELECT * FROM owner_data WHERE LastName = '$LstName' AND FirstName like '% FirstInitial%% %'") or die(mysql_error());  
    ?>

    <table width="600" border="1" cellspacing="0" cellpadding="2">
      <tr>
        <th scope="col">First</th>
        <th scope="col">Last</th>
        <th scope="col">Zip</th>
        <th scope="col">OwnerID</th>
      </tr>
    <?php  
        
    while($row mysql_fetch_array($result)) 
        { 
    ?>
       <tr>
        <td><?php echo " ".$row['FirstName'];?></td>
        <td><?php echo " ".$row['LastName'];?></td>
        <td><?php echo " ".$row['PostalCode'];?></td>
        <td bgcolor="#fffc00"><b><?php echo " ".$row['OwnerID'];?></b></td>
      </tr>              
    <?php  
        

      } 
    function 
    make_safe($variable)
      {
        
    $variable addslashes(trim($variable));
        return 
    $variable;
      } 

    ?>

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You've got too many wildcards. Try:

    Code:
    SELECT 
     * 
    FROM 
     owner_data 
    WHERE
     LastName = '$LstName'
    AND
     FirstName LIKE '$FirstInitial%'

  • #3
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,100
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Slight error, Kid Charming.

    Code:
    SELECT 
     * 
    FROM 
     owner_data 
    WHERE
     LastName = '$LstName'
    AND
     FirstName LIKE '%$FirstInitial%'
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That depends. If $firstInitial has to be the first character in the field's value, you don't want a wildcard in front of it. Otherwise, your select will retrieve all rows that contain $firstInitial anywhere.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •