user3655857
user3655857

Reputation: 55

Join 2 tables together in one query statement php

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.

Upvotes: 0

Views: 193

Answers (2)

GGio
GGio

Reputation: 7653

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.

Upvotes: 1

wvdz
wvdz

Reputation: 16641

Your syntax for the join is wrong.

Use:

"SELECT * FROM testcheck
INNER JOIN testdoc ON testcheck.Menu = testdoc.MENU AND page='$page'
");

Upvotes: 2

Related Questions