从数据库加载当前用户

时间:2018-05-14 21:18:13

标签: php mysql localhost

所以我有一个名为realtimeusage的表,它包含ID,KWH,UnitValue,AccessTIME我想通过他的“id”获取当前用户的使用情况我的代码的任何建议

<?php
session_start();
require_once('connect.php');

$_SESSION['id'] = $id;

// For display  Current user realtimeusage 
$displayquery = "SELECT * ";
$displayquery .= "FROM realtimeusage WHERE `id` = '".$_SESSION['id']."'";

$displayresult = mysqli_query($connection, $displayquery);
if (!$displayresult){
    die("database query failed");
}

?>

获取数据的表格:

<table>
    <thead>
        <tr>
            <th> AccountID</th>
            <th> KWH</th>
            <th>UnitValue</th>
            <th>AccessTIME</th>
        </tr>
    </thead>
    <tbody>
        <?php
            while ($rows= mysqli_fetch_assoc($displayresult)) {
         ?>
        <!--id-->
        <td><?php echo $rows["ID"]; ?></td>

        <!--User name-->
        <td><?php echo $rows["KWH"]; ?></td>

        <!--Full name-->
        <td><?php echo $rows["UnitValue"]; ?></td>

        <!-- Roles-->
        <td><?php echo $rows["AccessTIME"]; ?></td>

    </tbody>
    <?php } ?>
</table>

当我运行此代码时,它会显示表中的所有用法

<?php
session_start();
require_once('connect.php');

$username = $_SESSION['username'];
$roles = $_SESSION['roles'];


// For display realtimeusage
$displayquery = "SELECT * ";
$displayquery .= "FROM realtimeusage";

$displayresult = mysqli_query($connection, $displayquery);
    if (!$displayresult){
        die("database query failed");
    }

&GT;

1 个答案:

答案 0 :(得分:0)

你从哪里获得id的价值?我想id在$ _SESSION [&#39; id&#39;]中,如果用户已登录,并且要使用该ID,则需要将赋值语句更改为

$id=$_SESSION['id'];

在查询中使用$ id

$displayquery .= "FROM `realtimeusage` WHERE `id` = '".$id."'";