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>";
            }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • See the [documentation](http://php.net/manual/en/mysqli-result.fetch-array.php). `mysqli_fetch_array` returns an array or null, and you're trying to output as a simple variable. – cteski Nov 16 '17 at 23:00
  • 1
    Possible duplicate of [mysqli\_fetch\_array while loop columns](https://stackoverflow.com/questions/14456529/mysqli-fetch-array-while-loop-columns) – zod Nov 16 '17 at 23:08
  • Don't use two queries, use one query that joins the two tables. – Barmar Nov 16 '17 at 23:09
  • What's the purpose of the first two queries? – Barmar Nov 16 '17 at 23:10

2 Answers2

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>";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
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.

Aaron Magpantay
  • 405
  • 1
  • 5
  • 17
  • Still not working, `$receiver=mysqli_query($conn, "SELECT * FROM users WHERE userid='" . $rowtrans['idreceiver'] . "'"); $receivername = mysqli_fetch_array($receiver); echo "". $receivername .""; ` – Dizzy Tudor Nov 16 '17 at 23:04
  • @DizzyTudor That's the same as your question, not like the answer. – Barmar Nov 16 '17 at 23:08
  • @Barmar sorry, I ment `$receivername['name']`, I pasted it wrong. I have it correct now, but the field is still blank. – Dizzy Tudor Nov 16 '17 at 23:16