0

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?

5
  • You need to modify your sql query based on current user logged in :) It can be done Commented Dec 17, 2014 at 11:30
  • would select * from servers where owner = 'someone' work? How do I allow users to log in? then I can get their user ID? Commented Dec 17, 2014 at 11:31
  • no, it won't work, because you are not checking current user. I do log with user 'test' and i can see server with owner = 'test' right? only those? Commented Dec 17, 2014 at 11:32
  • thats exactly right. so my owners will have to match who is logged in. so im user "test" then it should do query, select * from servers where owner = 'test'. but how do i make users for logging in? Commented Dec 17, 2014 at 11:33
  • You need another table for logging in i suppose, the owner need to match the user table on name or id Commented Dec 17, 2014 at 11:34

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

ah, so I need a new table which will store passwords and usernames? and these usernames will match the owner name. But any guide on how to set up logging in through php?
There are lots of guide to set up login inside php, example this one -> codingcyber.com/simple-login-script-php-and-mysql-64/# But this got trouble with unsecure username and password, if you google on login with php and mysql you will find a lot of links :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.