Jump to content

Help on Joining Two MySQL Tables using PDO


Landslyde

Recommended Posts

$id = $_POST['view']; // Pass this to see which attendee was selected in the Selection box options

$stmt = $db->prepare('SELECT a.fname, a.lname, h.amount, h.subsidy, h.last_payment, h.amount_paid, h.balance
                FROM attendees As a INNER JOIN history AS h
                ON a.attendeeid = :id AND h.attendeeid = :id');
$stmt->bindValue(':id', $id, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();    



I know the last part of the query is wrong, the ON a.attendee = :id, etc. But how do I pass $id off to both a.attendeeid and h.attendeid and make it work? I need to be able to somehow make it

ON a.attendeeid = h.attendeeid');



...but that makes them devoid of $id. I've also tried

                
$stmt = $db->prepare('SELECT a.fname, a.lname, h.amount, h.subsidy, h.last_payment, h.amount_paid, h.balance
                FROM attendees As a INNER JOIN history AS h
                WHERE a.attendeeid = :id AND h.attendeeid = :id');
$stmt->bindValue(':id', $id, PDO::PARAM_INT);



but that offers nothing either. Any suggestions?

FROM attendees As a INNER JOIN history AS h
                ON a.attendeeid =  h.attendeeid

That tells it to match rows from a with those rows from h where the attendeeid values match

 

Now add a WHERE clause to specify what value you want to select

WHERE a.attendeeid = :id

EG

SELECT a.fname, a.lname, h.amount, h.subsidy, h.last_payment, h.amount_paid, h.balance
FROM attendees As a 
INNER JOIN history AS h ON a.attendeeid = h.attendeeid 
WHERE a.attendeeid = :id

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.