问题
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