Jump to content

Help on Joining Two MySQL Tables using PDO


Landslyde
Go to solution Solved by Barand,

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?

Link to comment
Share on other sites

  • Solution
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.