PHP - table from MySQL based on user logged in

断了今生、忘了曾经 提交于 2019-12-04 06:23:42

问题


Im new to PHP and mysql.

Currently I have a mysql database table which I query via a PHP script to display all the data via the browser. Currently, the format of the PHP table is same as the mysql table structure.

It displays the following currently.

Name      IPaddress   owner

test.com  192.1.12.1  someone

So three columns are displayed showing name, ipaddress, owner.

PHP snippet of how I get the data from mysql table:

$sql = "select * from servers;";
$result = $db->query($sql);

while($row = $result->fetch_assoc()){
echo '<td>' . $row['Name'] . '</td>';
echo '<td>' . $row['IPaddress'] . '</td>';
echo '<td>' . $row['Owner'] . '</td>';

}

So I go onto my PHP page and it displays all the results as expected. However, what I would like to do is for two users to be able to log in and if they are in Owner field they see their servers only. So basically display my servers to owners only and to owners, their server only.

Can someone help me out with this?


回答1:


I suppose to have the user that match owner (name or id will differ however) inside a var called $user

$sql = "SELECT * FROM servers WHERE owner='".$user."'"; //For $user = string
$sql = "SELECT * FROM servers WHERE owner=".$user; //For $user = integer


来源:https://stackoverflow.com/questions/27524562/php-table-from-mysql-based-on-user-logged-in

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!