简体   繁体   中英

want to fetch data from two tables in mysql

hello i want to display data from two tables meaning this table i need from it amount and created and the user_id i want to get the username from other table name is users withdraw table users table

<?php
$servername = "localhost";
$username = "aa";
$password = "aat&IpnFt";
$dbname = "aa";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}



$sql = " SELECT withdraw.amount, withdraw.account, withdraw.created
FROM withdraw
INNER JOIN users ON withdraw.user_id=users.username; ";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["username"]. "   Name: " . $row["amount"]. "<br> " . $row["account"]. " " . $row["created"]. "<br>";
    }
} else {
    echo "Nothing Found!";
}
$conn->close();
?> 

You didn't select username of table2 and compare withdraw.user_id with users.username

Try:

$sql = " SELECT withdraw.amount, withdraw.account, withdraw.created, users.username
FROM withdraw
INNER JOIN users ON withdraw.user_id=users.id; ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM