0

Im trying to join two tables together becouse 2 columns match and i need info from second table to display the content. When i pressing a link with ?p=1a i want content to show and this info i have on the second table but not the first one. Where table 1 and table 2 match on column Menu. I have shorten down on some code/table info becouse its not relevant for this problem. im then displaying the info with mysql_fetch_assoc.

TABLE 1

MENU | subtitle | firstname |info

info | contact

word | woord

TABLE 2

MENU | page |

info | 1a

word | 1b

My code:

if(isset($_GET['p'])){
    $page = $_GET['p'];

    $find = mysqli_query("SELECT * FROM testcheck, testdoc INNER JOIN testdoc ON testcheck.Menu = testdoc.MENU AND page='$page' ");
    while($row = mysqli_fetch_assoc($find)){
        $subtitle = $row['subtitle'];
        $firstname = $row['firstname'];
        echo $firstname
    }
}

Problem is correct kinda now but only letters work fine but when i combine page='1a' for example everything stop works.

  • 1
    You should really read this [Question and Answer](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables) that I put together a while back to help with cases just like this - a simple muck up in syntax, answers but no really really detailed explanation of what you are doing incorrectly (or how the rest of it works). At the moment you are mixing ANSI-89 and ANSI-92 SQL formats. – Fluffeh May 21 '14 at 13:14
  • Remove the `, testdoc` from your query right after `FROM testcheck` – Jay Blanchard May 21 '14 at 13:18
  • why will this not work but $visadok = mysqli_query($conn,"SELECT * FROM testdok LEFT JOIN testcheck ON testdok.rubrik=testcheck.rubrik WHERE meny='1a'") but $visadok = mysqli_query($conn,"SELECT * FROM testdok LEFT JOIN testcheck ON testdok.rubrik=testcheck.rubrik WHERE meny='QMA '") does work when i have both these in my table? – user3655857 May 21 '14 at 14:13

2 Answers2

2

Your syntax for the join is wrong.

Use:

"SELECT * FROM testcheck
INNER JOIN testdoc ON testcheck.Menu = testdoc.MENU AND page='$page'
");
wvdz
  • 16,251
  • 4
  • 53
  • 90
1

Read more about LEFT JOIN and RIGHT JOIN. In your case you would need RIGHT JOIN to get data that is in second table but not in first.

Like popovitsj suggested your syntax is also wrong.

Correct syntax:

SELECT * FROM testcheck
INNER JOIN testdoc ON testcheck.Menu = testdoc.Menu AND page='$page'

Also note that you are using MENU upper case which is not a problem on windows but will be a problem on unix, it will give you an error Column Not Found.

EDIT

If your table has column MENU then it should be fine.

GGio
  • 7,563
  • 11
  • 44
  • 81