help with mysql (searching)

Discussion in 'PHP' started by killaklown, Jul 4, 2006.

  1. #1
    Im trying to make my code so if you type www.license.com OR http://www.license.com it will say its found (if it finds www.license.com or http://www.license.com in the mysql database) and not found if its not in the database.

    If i type www.license.com in the field box and click submit, it says its found, but if i type http://www.license.com it says its not found.

    Its kind of confusing and i cant really make it easier to understand... hope you can.

    Heres the mysql code i have:
    
    $sdb = $_POST['check'];
    $sdbht = 'http://'.$_POST['check'].'';
    $query = "SELECT * FROM licensed WHERE domain = '$sdb' OR domain = '$sdbht'";
    $result = mysql_query($query); 
    $myrecord = mysql_fetch_array($result);
    
    Code (markup):
     
    killaklown, Jul 4, 2006 IP
  2. Travis

    Travis Peon

    Messages:
    539
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $sdb = $_POST['check'];
    $sdbht = 'http://'.$_POST['check'].'';
    $query = "SELECT * FROM licensed WHERE domain LIKE '$sdb' OR domain LIKE '$sdbht'";
    $result = mysql_query($query);
    $myrecord = mysql_fetch_array($result);


    Will do the trick I think?
     
    Travis, Jul 4, 2006 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    killaklown, Jul 4, 2006 IP
  4. linkwebd

    linkwebd Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this:


    $sdb = str_replace("http://", "", $_POST['check']); // removes the http:// if entered into the form
    $query = "SELECT * FROM licensed WHERE domain = '$sdb';
    $result = mysql_query($query);
    $myrecord = mysql_fetch_array($result);
     
    linkwebd, Jul 4, 2006 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    it gives me
     
    killaklown, Jul 4, 2006 IP
  6. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The line starting with $query is missing the end quote on the SQL String. It should be:

    $query = "SELECT * FROM licensed WHERE domain = '$sdb'";

    I'm not sure if that caused the error you're getting, I got a different one, but once I fixed it, it works fine.
     
    ip076, Jul 4, 2006 IP
  7. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #7
    thanks, works :D is there a way to remove "http://" and/or "www." and/or "/"?
     
    killaklown, Jul 5, 2006 IP
  8. danielbruzual

    danielbruzual Active Member

    Messages:
    906
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    70
    #8
    I think you can just use str_replace() again:

    $sdb = str_replace("http://", "", $_POST['check']); // removes the http:// if entered into the form
    $sdb = str_replace("www.", "", $sdb); //remove www.
    $sdb = str_replace("/", "", $sdb); //remove slashes
    $query = "SELECT * FROM licensed WHERE domain = '$sdb'";
    $result = mysql_query($query);
    $myrecord = mysql_fetch_array($result);
    PHP:
    Note: only remove the slashes if you are working with pages without a subdirectory in their url (eg. http://www.page.com/users/ needs the slashes otherwise it won't load the correct page [page.comusers does not exist])

    EDIT: I think it would not be that hard to write a function that would remove only the last slash of an url. Let us know if that's what you are looking for.
     
    danielbruzual, Jul 5, 2006 IP
  9. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #9
    I didnt know you can use the same variable twice ($sdb) thanks (i actually did $sdb, $sdbn, $sdbf)

    I dont need to be able to enter sub directories.
     
    killaklown, Jul 5, 2006 IP