Ask Experts Questions for FREE Help !
Ask
    eruckus's Avatar
    eruckus Posts: 6, Reputation: 1
    New Member
     
    #1

    Feb 1, 2008, 05:04 PM
    Complicated database querys
    Greetings!

    I am trying to query my database through php but its starting to get real complicated. This is the setup: a user visits page1.php to search for a name in the database. I want the user to be able to search for this name by user name, first name or last name. Say they type in "John Smith" into the form...

    The script then querys the database: SELECT * FROM users WHERE ((username LIKE '%John%') OR (firstname LIKE '%John%') OR (lastname LIKE '%John%')) OR ((username LIKE '%Smith%') OR (firstname LIKE '%Smith%') or (lastname LIKE '%Smith%'))

    So I'm wondering is there an easier way to query the database and get the same results? I am planning to use another search for business names where users might type in more than two words and I would hate for the query to get any more complicated than it already is.

    Many thanks in advance!
    - e.ruckus
    MommyLeah's Avatar
    MommyLeah Posts: 47, Reputation: 3
    Junior Member
     
    #2

    May 4, 2008, 08:37 PM
    I'm guessing that your search form has separate fields for username, name, business name, etc. You don't have to write a query that checks every field that they can search, just the ones that they entered. You can use PHP to build the query as a string. The PHP will be longer, but your SQL will be as short as possible. It might look something like this if you are getting your search variables from the POST data:

    // the base of the query
    // (1=1 just makes it easy to append more sql and know you are safe to use AND)
    $sql = 'SELECT * FROM users WHERE 1=1 ';

    // build sql if the username is found (repeat this for every field)
    if( $_POST['username'] != '' ) {
    $username = addslashes($_POST['username']);
    $sql .= " AND username = '$username' ";
    }

    // execute the query.

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Why is life so complicated [ 10 Answers ]

Why is life so complicated why does everything need to be so messy like why can't life be simple why do people need to like do what they want even if it would hurt someone why do people say things they don't really mean but still say them or if they do mean them why do they need to say it.is life...

Telling my ex to get over me - but its more complicated than that! [ 8 Answers ]

A while ago, I was going out with this guy, and my parents are strict and don't want me to have a boyfriend until I'm older. They read my text messages and found out about a previous boyfriend and the one I was currently with. They grounded me, and they don't trust me, however I still talk to him...

Complicated situation [ 1 Answers ]

Dear sir, I have a friend of mine who was here on h1B visa before 3 yrs due to slow down in computer field he was not employed and did not pay any taxes, He is now working and his employer is going to pay all the taxes,Is this going to be problem for him since no tax was reported to IRS before....


View more questions Search