cream
cream

Reputation: 1129

Why isn't my SQL LEFT JOIN query working?

It is returning 0 rows when there should be some results.

This is my first attempt at using JOIN but from what I've read this looks pretty much right, right?

"SELECT pending.paymentid, pending.date, pending.ip, pending.payer, pending.type, pending.amount, pending.method, pending.institution, payment.number, _uploads_log.log_filename 
FROM pending 
LEFT JOIN _uploads_log 
ON pending.paymentid='".$_GET['vnum']."' 
AND _uploads_log.linkid = pending.paymentid"

I need to return the specified values from each table where both pending.paymentid and _uploads_log.log_filename are equal to $_GET['vnum]

What is the correct way to do this? Why am I not getting any results?

If someone more experienced than me could point me in the right direction I would be much obliged.

EDIT

For pending the primary key is paymentid, for _uploads_log the primary is a col called log_id and log_filename is listed as index.

Upvotes: 1

Views: 729

Answers (2)

John Woo
John Woo

Reputation: 263713

Try this

 SELECT  pending.paymentid, 
         pending.date, 
         pending.ip, 
         pending.payer, 
         pending.type, 
         pending.amount, 
         pending.method, 
         pending.institution, 
         payment.number, 
         _uploads_log.log_filename 
FROM     pending 
         LEFT JOIN _uploads_log 
              ON _uploads_log.linkid = pending.paymentid
WHERE   _uploads_log.log_filename = '" . $_GET['vnum']  . "' 

Your current query is vulnerable with SQL Injection. Please take time to read the article below.

Best way to prevent SQL injection in PHP?

Upvotes: 5

hol
hol

Reputation: 8423

The ON clause only should have the condition to link the two tables especially if it is LEFT JOIN. The WHERE clause then has the actual condition. Otherwise you will get nothing if there is no corresponding entry in _uploads_log. It also is more easy to read in my opinion.

As another remark. It is always better to work with bind parameters to avoid SQL injection.

Upvotes: 0

Related Questions