0

I have 2 tables: users and transactions

In users I have: userid, name, email

And in transactions I have: id, idsender, idreceiver

I want to make a log-table showing all the transactions and I want them to be displayed like: Transaction ID - SENDER'S NAME - RECEIVER'S NAME

I tried like this but it doesn't seem to work, at "Receiver" it doesn't show anything .. :

echo "<table>";
echo "<tr>";
echo "<th>Transaction ID</th>";
echo "<th>Sender</th>";
echo "<th>Receiver</th>";
echo "</tr>";

    $resultuser = mysqli_query($conn, "SELECT * FROM users"); 
    $rowuser=mysqli_fetch_array($resultuser);

    $resulttrans = mysqli_query($conn, "SELECT * FROM transactions"); 
    $rowtrans=mysqli_fetch_array($resulttrans);



    $ress=mysqli_query($conn, "SELECT * FROM transactions");

            while($row=mysqli_fetch_array($ress)){
                echo "<tr>";
                echo "<td>".$row['id']."</td>";
                echo "<td>"." same as receiver"."</td>";
            $receiver=mysqli_query($conn, "SELECT name FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'");
            $receivername = mysqli_fetch_array($receiver);

                echo "<td>". $receivername ."</td>";
                echo "</tr>";
            }
4
  • See the documentation. mysqli_fetch_array returns an array or null, and you're trying to output as a simple variable. Commented Nov 16, 2017 at 23:00
  • 1
    Possible duplicate of mysqli_fetch_array while loop columns Commented Nov 16, 2017 at 23:08
  • Don't use two queries, use one query that joins the two tables. Commented Nov 16, 2017 at 23:09
  • What's the purpose of the first two queries? Commented Nov 16, 2017 at 23:10

2 Answers 2

1

Don't use two queries. Join the two tables in one query:

$ress = mysqli_query("SELECT t.id, u1.name AS sendername, u2.name AS receivername
    FROM transactions AS t
    JOIN users AS u1 ON u1.userid = t.idsender
    JOIN users AS u2 ON u2.userid = t.idreceiver");
while ($row = mysqli_fetch_assoc($ress)) {
    echo "<tr>";
    echo "<td>".$row['id']."</td>";
    echo "<td>" . $row['sendername'] ."</td>";
    echo "<td>" . $row['receivername'] . "</td>";
    echo "</tr>";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You are echoing from fetched array in $receivername = mysqli_fetch_array($receiver);

you should do echo "<td>". $receivername['name'] ."</td>";

Note: Fetched array from mysqli_fetch_array() can be echoed using indexes by number $receivername[0] or by string $receivername['name'], that is where you are missing here.

3 Comments

Still not working, $receiver=mysqli_query($conn, "SELECT * FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'"); $receivername = mysqli_fetch_array($receiver); echo "<td>". $receivername ."</td>";
@DizzyTudor That's the same as your question, not like the answer.
@Barmar sorry, I ment $receivername['name'], I pasted it wrong. I have it correct now, but the field is still blank.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.