Searching multiple tables

Discussion in 'MySQL' started by Rian, Feb 7, 2010.

  1. #1
    Okay, so no one here have sufficient knowledge on my previous question. Hopefully someone can help with this one.

    I want to search 3 tables, all with the same fields. My users will search only one field.

    This is what code I have, but it does not work.

    <?php
    $link = mysql_connect ("localhost", "xxxx","xxxx")  or die (mysql_error());
    mysql_select_db ("xxxxx",$link);
    $term = $_POST['term'];
    {
    $sql = mysql_query("SELECT * FROM 'data2','data3' WHERE ID_NUMBER like '%$term%'");  
    $coloum = mysql_fetch_array($sql)
    ?>
    Code (markup):
    Any suggestions?
     
    Rian, Feb 7, 2010 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    tip : use ` instead '
    <?php
    $link = mysql_connect ("localhost", "xxxx","xxxx") or die (mysql_error());
    mysql_select_db ("xxxxx",$link);
    $term = $_POST['term'];
    {
    $sql = mysql_query("SELECT * FROM `data2`,`data3` WHERE ID_NUMBER like '%$term%'");
    $coloum = mysql_fetch_array($sql)
    ?>
     
    crivion, Feb 7, 2010 IP
  3. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    my suggestion is you should FULL JOIN all 3 tables, the key is the same field


    <?php
    $link = mysql_connect ("localhost", "xxxx","xxxx")  or die (mysql_error());
    mysql_select_db ("xxxxx",$link);
    $term = $_POST['term'];
    
    $query = " SELECT * FROM data1 FULL JOIN data2 ON data1.ID_NUMBER = data2.ID_NUMBER FULL JOIN data3 ON data1.ID_NUMBER = data3.ID_NUMBER WHERE COALESCE(data1.ID_NUMBER, data2.ID_NUMBER,data3.ID_NUMBER) LIKE '%term%' ";
    
    $sql = mysql_query($query);  
    $coloum = mysql_fetch_array($sql)
    ?>
    Code (markup):
    okay hope it's work.
     
    anxggxun, Feb 7, 2010 IP
  4. Rian

    Rian Well-Known Member

    Messages:
    1,763
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    125
    #4
    Thanks anxggxun, but I get an Internal 500 error when trying to do this,

    crivion, I am not sure what you are telling me to do.

    Thanks in any event,
     
    Rian, Feb 7, 2010 IP
  5. anxggxun

    anxggxun Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    it works when i was try to do something like that, hmm i never thought it could make a http error, i'll try to find its mistakes...
     
    anxggxun, Feb 8, 2010 IP
  6. Rian

    Rian Well-Known Member

    Messages:
    1,763
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    125
    #6
    Hi anxggxun

    I have figured out why the 500 error happens. It is fixed, but no results are displayed when I make use of the ample above.
     
    Rian, Feb 8, 2010 IP