Skip to content
Advertisement

Showing two different values depending on SESSION value in INNER JOIN

I have two different tables, one named users, and another named transactions. Transactions contains wallet1, wallet2, amount. Users contains user details such as firstname, lastname, and wallet. I am trying to display the corresponding first name and last name, depending on whether or not the SESSION_wallet is equal to wallet1 or wallet2 within transactions. I tried searching for a while, and came up with a solution for showing the correct display name for the first and last name making the transfer, however, I am trying to make it display the correct value for “Transfer to:”

Here is some of my code to get a better understanding of what I mean:

MySQLi Query:

PHP Code:

The table that needs to display the transfer from, and transfer to:

Right now my tables are only showing the first and last name of the user that made the Transfer, however, I need it to display the first and last name of the user that the transaction is made to as well. The else if code is working correct, but the first part is not showing the corresponding value.

Advertisement

Answer

You will need to JOIN your transactions table to your users table twice, once to get each users name. Then to avoid duplicate column names overwriting the results in the output array, you will need to use column aliases. Something like this should work:

Then you can access each user’s names as $row['w1_firstname'] etc.:

Note that ideally you should use a prepared query for this, for example:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement