想要从mysql中的两个表中获取数据

时间:2018-11-03 19:43:33

标签: php mysql

你好,我想显示两个表中的数据 含义 我需要从这张桌子的数量和创建 和user_id 我想获取用户名 从其他表名是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();
?> 

1 个答案:

答案 0 :(得分:0)

您没有选择table2的用户名,而是将withdraw.user_id与users.username进行了比较

尝试:

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