如何使用user_id从另一个表中获取数据

时间:2016-02-26 07:18:57

标签: php html mysql database

作为用户,我希望在登录时查看我的个人资料。我想要查看的数据来自另一个使用user_id的表。

我已尝试过以下代码,但我得到了该表的所有数据。我尝试过使用session_id,但它没有用。

这是我的代码:

<div>
    <table style="border: 2px solid black;">
        <tr>
            <th>Users Id</th>
            <th>Language from</th>
            <th>Language to</th>
            <th>Category name</th>
            <th>Message</th>
            <th>File Name</th>
            <th>Number of words of file</th>
            <th>Count of words paste</th>
            <th>Total</th>
        </tr>
        <?php
        //$user_id = $_SESSION['user_id'];
        $res = mysql_query("SELECT * from checkout JOIN users WHERE users.user_id = checkout.user_id");
        while($row = mysql_fetch_array($res))
        {
            ?>
            <tr>
                <td><?php echo $row['user_id']; ?></td>
                <td><?php echo $row['language_from']; ?></td>
                <td><?php echo $row['language_to']; ?></td>
                <td><?php echo $row['category_name']; ?></td>
                <td><?php echo $row['message']; ?></td>
                <td><?php echo $row['file']; ?></td>
                <td><?php echo $row['file_count']; ?></td>
                <td><?php echo $row['word_count']; ?></td>
                <td><?php echo $row['total']; ?></td>
            </tr>
            <?php
        }
        ?>
    </table>
</div>

请尽力帮助我,伙计......

1 个答案:

答案 0 :(得分:1)

我不明白,如果你已经有了user_id,那就

SELECT * FROM users WHERE id = <your user_id>

或者修复您的第一个请求

SELECT u.* 
FROM users AS u
INNER JOIN checkout AS c 
ON u.id = c.user_id
WHERE u.id = <id>
相关问题