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

Thread: mySQL, PHP

  1. #1
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default mySQL, PHP

    Hey, is there an easy way to "search" for the same term in 5 different tables in a mysql database?
    Last edited by nikomou; 03-14-2006 at 10:35 PM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Umm... just get the values of all the items.

    do something like this:


    1. GET DATA FROM DATABASE, storing it as an array.
    2. Compare to the 'term' for each item in the array.
    3. Loop 1-2 for all tables of the database. OR copy/paste.

    The only complex thing about what you said is different tables... just repeat the command 5 times.... just cut/paste. Or, if you feel fancy, make a while loop or something... whatever.

  3. #3
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    PHP Code:
    mysql_connect("host","username","password");
    mysql_select_db("dbname");
    $tables = array("table1","table2","table3","table4","table5");
    $search "term";
    $field "name";
    foreach(
    $tables as $mysql){
    $query1 "SELECT * FROM `$mysql` WHERE `$field` LIKE '%$search%'";


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

    Default

    Code:
    $tables = array("table1","table2","table3","table4","table5");
    $search = "term";
    $field = "name";
    foreach($tables as $mysql) {
      $query1 = "SELECT * FROM `$mysql` WHERE `$field` LIKE '%$search%';";
      mysql_query($query1);
    }
    The ending quote. Small, but oh so vital.
    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!

  5. #5
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks for that.

    What i want to do with the results is to say which table the result was found in. and to "echo" the results, like this:

    *Search term* Found in *Table 1*

    and have the results seperated by a pipeline " | "

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

    Default

    PHP Code:
    foreach($tables as $mysql) {
      
    $query1 "SELECT * FROM `$mysql` WHERE `$field` LIKE '%$search%';";
      
    $res mysql_query($query1);
      
    $rs mysql_fetch_array($res);
      if(
    $rs) {
        echo(
    $search ' found in ' $mysql "\n\n");
        while(
    $rs mysql_fetch_array($res))
          foreach(
    $res as $col)
            echo(
    $col "|");
      }

    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!

  7. #7
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    Thanks tway!

    Is it possible to have it to search for all the items in an other table? rather than searching for 1 specific term?

    Does that make sence?!

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

    Default

    Not really. Can you clarify?
    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!

  9. #9
    Join Date
    Aug 2005
    Posts
    174
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default

    ok, instead of having the search querie ($search), i want to use the contents from an other table (tbmain) in the database.

    e.g.

    xxx found in yyy | xxx found in yxy | xxx found in xyx | xxx found in xzx | xxx found in yzy
    bbb found in yyy | bbb found in yxy | bbb found in xyx | bbb found in xzx | bbb found in yzy
    ccc found in yyy | ccc found in yxy | ccc found in xyx | ccc found in xzx | ccc found in yzy

    Where, xxx, bbb, ccc are located in the other table (tbmain)

    Kinda hard to explain!

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

    Default

    Kinda hard to explain!
    Evidently... you want to find a certain field, then, based on that field, display data from a row in another table that also contains the data in that field?
    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!

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
  •