PHP
PHP

Reputation: 73

MySQL join and associative array?

I am trying to do a MySQL join like so,

$q = $dbc -> prepare("SELECT m.* a.username FROM mailbox m JOIN accounts a (m.msgFrom = a.id) WHERE msgTo = ? AND sent = '0'");
$q -> execute(array($user['id']));
while ($msg = $q -> fetch(PDO::FETCH_ASSOC)) {
     echo $msg['username'];
}

I would like to pull the users' username from the table accounts rather than just display the id from the table mailbox, I get the array performing a while loop on the query,

This doesn't seem to work though, any ideas?

Upvotes: 2

Views: 446

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79929

You forgot the on in the join condition try this:

SELECT m.*, a.username 
FROM mailbox m JOIN accounts a on (m.msgFrom = a.id) 
WHERE msgTo = ? AND sent = '0'

Upvotes: 1

Related Questions