selecting from more than 1 table

Discussion in 'PHP' started by promotingspace.net, Aug 27, 2007.

  1. #1
    Hi
    I need to use info from 2 tables. i use this code:
    
    $sql2="SELECT l.id , l.advertiser_id , l.title , l.url , l.displayurl , l.description , l.adult , l.keywords , l.maxb , a.id , a.balance FROM listing l ,advertiser a WHERE keywords LIKE '%$keyword%' and a.balance>5 and l.advertiser_id=a.id ORDER BY maxb DESC  LIMIT $lim ";
    PHP:
    then i need to use mysql_fech_array you know.
    $row2=mysql_fetch_array($result2)
    then how should i specify array elements?
    we used $listing=$row2['id']; what now? when i use $listing=$row2['l.id']; it seems not working
     
    promotingspace.net, Aug 27, 2007 IP
  2. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #2
    You're probably better off using aliases in your sql query.

    SELECT 1.id id, 1.advertiser_id ad_id, a.id anotherId FROM ....

    Notice the (space)(alias name) after each column in your select.
     
    tandac, Aug 27, 2007 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    i used this code:
    
    $sql2="SELECT l.id lid, l.advertiser_id advertiser, l.title title, l.url url, l.displayurl display, l.description desc, l.adult adult, l.keywords keywords, l.maxb maxb, a.id id, a.balance balance FROM listing l ,advertiser a WHERE keywords LIKE '%$keyword%' and balance > 5 and advertiser=lid ORDER BY maxb DESC  LIMIT $lim ";
    
    PHP:
    it says:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, l.adult adult, l.keywords keywords, l.maxb maxb, a.id id,
    where i am wrong?
     
    promotingspace.net, Aug 27, 2007 IP
  4. tandac

    tandac Active Member

    Messages:
    337
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Desc is a reserved word you cannot use it as a column alias.
     
    tandac, Aug 27, 2007 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    anyway, using aliases or not, every fetch in your query is stored into an array and you can identify your field results by a numeric index, i.e., $row2[0] instead of $row2['id']
     
    webrickco, Aug 28, 2007 IP