Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: MySQL select where any = something

  1. #1
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default MySQL select where any = something

    Hey, I have quite a few columns, and I'm in the process of making a search bar, but I want to have it to so that when the database is queried instead of having to say:

    mysql_query("SELECT * FROM table WHERE column1 = '$search' OR column2 = '$search' OR column3 = '$search' OR etc etc...");

    I want to just be able to say:

    mysql_query("SELECT * FROM table WHERE any of the columns = '$search'");

    Is that possible, or do I have to list every column?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    As far as I'm aware, there's no method to do this in MySQL. I think that it's probably an indication of bad design, generally.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    How else could you search through all columns and it not be bad design :S

  4. #4
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    To make it somewhat easier, you could make an array of the column names, then form a loop which ties the column names into a string. This string could then be put into mysql_query() for execution.

    Example:

    PHP Code:
    <?php

    // search query
    $search "dogs";

    // table name
    $table "tableName";

    // column names
    $c = array(
    "column0",
    "column1",
    "column2",
    "column3",
    "column4"
    );

    // mysql query string
    $str "SELECT * FROM `{$table}` WHERE ";

    // ties in columns with mysql query string
    for($i 0$i count($c); $i++) {
      if(
    $i == (count($c)-1)) { $str .= ("`{$c[$i]}` = '{$search}'"); }
      else { 
    $str .= ("`{$c[$i]}` = '{$search}' OR "); }
    }

    // executes the mysql query string (uncomment to execute)
    // mysql_query($str);

    // echoes query string
    echo $str;

    ?>
    But, as you guys said, it may be bad deisign...
    Last edited by magicyte; 02-18-2009 at 12:43 AM.

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    How else could you search through all columns and it not be bad design :S
    No, you're missing the point. If you have to search through all columns, you've some design issues.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Okay... But then how could I redesign it in a way where it wouldn't need to query all columns but still get all that information, I just don't see how that's possible

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well that depends on your application.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    It's for my A2 coursework, my client (runs a business making covers for boats) keeps track of customers' invoices and there's information like:

    Customer ID
    Boat type
    Name
    Description of job
    Date completed
    Pattern location (L7 for example, where the physical pattern for the cover is)


    I want him to be able to put in any sort of search term, so if a customer rings up and they have their invoice, they can tell him their customer ID and he can just put it in, and then the record with that ID will show up.

    But then some customers may not have their invoice ID, so they can just tell him their name and then he can put that in and search for the records that way.

    That's why I need it to span across multiple columns, so that he can search for different types of information, name, what their boat was etc etc...

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Pattern location (L7 for example, where the physical pattern for the cover is)
    What if the customer ID is 7? And here we see the issue with this approach. Have the user choose what they want to search — don't do it for them. By all means allow them to select multiple columns, but make it explicit.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Quote Originally Posted by Twey View Post
    What if the customer ID is 7?
    I don't think you understand, it's fine if the customer ID is 7, I'm letting the user search for anything they want, they could put in anything, ID, name, pattern location... The L7 is just an example.

    The user has a load of shelves with patterns in them, ranging from A1 - L9 and counting, this is just so he knows where to get the pattern from if he needs to remake a cover in the future for the same boat.

Bookmarks

Posting Permissions

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