duandao1931 2015-12-07 03:20
浏览 39

在PHP中过滤MySQL数据只显示当前会话用户的数据?

I have the following PHP script that selects MySQL data and displays all the different users' data in a table.

What I need is to limit this data to only that of the logged-in user for that session. As in only the single ID, name and email of the person logged in instead of the table showing all user data.

I assume I need to "SELECT * FROM usertable where (something) = (something)", I'm just not sure where to go from here.

Any help would be appreciated.

<?php
$con=mysqli_connect("localhost","myusername","mypassword","mydatabase");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM usertable");

echo "<table border='1'>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";

while($row = mysqli_fetch_array($result)) 
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['emailaddress'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>
  • 写回答

2条回答 默认 最新

  • duanlieshuang5330 2015-12-07 04:09
    关注

    Hello!


    Well you are right you need a WHERE clause there in your statement.

    If you have someone logged in and are looking for that persons name is logged in first store the id in a variable such as a $_SESSION variable your statement would look like SELECT * FROM usertable WHERE id = {$_SESSION['id']}

    So first you would need a function when a user logs to store it

    $_SESSION['id'] = row['id']

    or If your looking for a specific first/lastname combo:

    "SELECT * FROM usertable WHERE firstname = '{$firstname}' AND lastname = '{$lastname}' 
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?