Работа с документами для пользователей

How can I get a list of user accounts using the command line in MySQL?

I’m using the MySQL command-line utility and can navigate through a database. Now I need to see a list of user accounts. How can I do this? I’m using MySQL version 5.4.1.

@Mustapha Why the title change? This answers here are SQL you can run from anywhere, not just a command line. And what does that tag add to the question?

The purpose of my edit was to maintain consistency between the title and the description, but I think you have a good point. Make your edit Mr. @Rup

16 Answers 16

SELECT User FROM mysql.user; 

Which will output a table like this:

As Matthew Scharley points out in the comments on this answer, you can group by the User column if you’d only like to see unique usernames.

I think it may be necessary to group on User too, to only get unique user values, since there’s a seperate row for each user @ host entry.

Читайте также:  Http sch1106 mosuzedu ru edresurs html

@barrycarter DELETE FROM mysql.user; better have WHERE user=’someuser’ and host=’somehost’; If you do DELETE FROM mysql.user; , all users are gone. Logins after the next mysql restart or FLUSH PRIVILEGES; eliminate users from memory. Here is an example of one of my posts on doing DELETE FROM mysql.user responsibly : dba.stackexchange.com/questions/4614/…

I find this format the most useful as it includes the host field which is important in MySQL to distinguish between user records.

select User,Host from mysql.user; 

@Packer the host comes into play when you are connecting from a different server. It is possible to grant different access to ‘packer’@’example.com’ and ‘packer’@’google.com’

A user account comprises the username and the host level access.

Therefore, this is the query that gives all user accounts

SELECT CONCAT(QUOTE(user),'@',QUOTE(host)) UserAccount FROM mysql.user; 

This is basically the same as spkane’s answer. What is the benefit of concatenating the user and host columns?

One example: the user@host format is used for setting passwords. Omitting the host from the SET PASSWORD command produces an error. SET PASSWORD FOR wordpressuser = PASSWORD(‘. ‘); produces the error ERROR 1133 (42000): Can’t find any matching row in the user table . Include the host and it works. SET PASSWORD FOR wordpressuser@localhost = PASSWORD(‘. ‘); produces Query OK, 0 rows affected (0.00 sec) .

Best answer, whatever the naysayers may complain about. Only thing i’d change were appending an ORDER BY user to it.

Источник

Show all users in a table and delete those selected

how can I show all the users in the users table in the database in a html table? I have to show all the users in a html table and each user can be selected using a check box. The selected users are then deleted from the database. I am using the technique mvc and I have a page «DeleteUser» that contains only the html, a controller and a public file. View/DeleteUser.php contains:

  
# Username First name Last name City Gender E-mail Check
 $controller = new DeleteUserController(); $view = $controller->invoke(); $view->render(); 
 class DeleteUserController < public $user; public $users; public $userRepository; public $view; public function __construct() < $username = $_SESSION['username']; $this->userRepository = new UserRepository(); $this->user = $this->userRepository->findByUsername($username); $this->users = array(); $this->view = new View('DeleteUser'); $db = Database::getInstance(); > public function listUsers() < $this->users = $this->userRepository->fetchAll(); foreach($this->users as $u) < $str = "getId() ?> "; > > public function deleteUsers() < if(isset($_POST['submit-deleteuser'])) < $del_id = $_POST['users']; foreach($del_id as $value) < $value->delete(); > > > public function work() < $this->listUsers(); $this->deleteUsers(); > public function invoke() < $this->work(); $this->view->setData('user', $this->user); return $this->view; > 
   "; > > ?> 
# Username Fisrt name Last name City Gender E-mail Check
$row[id] $row[username] $row[fisrt_name] $row[last_name] $row[city] $row[gender] $row[email]

But now I do not know how to do. The two methods listUsers() and deleteUsers() are wrong and do not know how to complete them or correct them. First it was simple, I put the php code that showed users in the html tag that I wanted (

.

) but now? I do not know how to handle this thing. Give me advice? Thank you 🙂 fetchAll() is:

 public function fetchAll()< $db = Database::getInstance(); $stmt = $db->prepare("SELECT * FROM `users`"); $stmt->execute(); $users = array(); while($result = $stmt->fetch()) < $users[] = User::load($result); >return $users; > 

Источник

How do I get a list of active users with PHP?

I currently maintain a DB table of users, when after logging in I update the table with their ID and login_time. This works to a point but currently I can’t tell if the user has been active since the login or for how long. Is there a better way to get a complete list of users that have been active in the past X minutes?

You could add a «last seen» column in the table and refresh that every time the user makes a new request

Making a DB request should be okay for most sites. If you have huge traffic, you might want to switch to a memcache at some point, but a database call shouldn’t cause any performance problems for a site with low to medium traffic. Any CMS-driven site without static caching makes dozens of them on every request

@Paul, The end of a «visit» can only be defined by an explicit logout or a timeout. Take a look here: stackoverflow.com/questions/679657/find-number-of-open-sessions

2 Answers 2

The best way to get what you need would be a «Last Activity» column in the users table. You would just update it whenever a user access a page. Depending on what information you need it could replace the login_time column or it could be a new column.

You’ll have to keep track of when the user made their last request in your database as a separate table or column. You can then formulate a query to select, e.g. all users that have made a request in the last 5 minutes.

PHP itself does not store — or care for — that kind of information. Unless you happen to have your own session management module which does store this kind of information, then you could use data from that.

Источник

Как сделать вывод пользователей из БД и их данные?

Как сделать вывод пользователей из БД
Здравствуйте помогите пожалуйста как вывести список пользователей из бд CREATE TABLE IF NOT.

Как сделать вывод аватарки всех пользователей?
Нужно сделать вывод аватарки,получилось сделать только вывод название загруженого файла но не саму.

Как сделать вывод всех пользователей пока поле пустое?
Есть поиск пользователей написан на php,html,js при вводе в поле имя выводит пользователя но пока.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function LoadDataUsers() // Загрузка списка юзеров { $query = mysql_query("SELECT id, fio FROM sobstvenniki"); $array = mysql_fetch_array($query); do { echo "\"?id_user=".$array['id'].">".$array['id']." ".$array['fio'].""; } while($array = mysql_fetch_array($query)); } function CurrentUser($id_user) //Вывод инфы по конкретному пользователю { if(empty($id_user)) { echo "Юзер не выбран"; } else { $query = mysql_query("SELECT * FROM sobstvenniki WHERE "); $array = mysql_fetch_array($query); return $array; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
 session_start(); ?>   

Здраствуйте!

$db = mysql_connect("localhost", "root"); mysql_select_db("diplomnaya",$db); function LoadDataUsers() // Загрузка списка юзеров { $query = mysql_query("SELECT id, fio FROM sobstvenniki"); $array = mysql_fetch_array($query); do { echo "\"?id_user=".$array['id'].">".$array['id']." ".$array['fio'].""; } while($array = mysql_fetch_array($query)); } function CurrentUser($id_user) //Вывод инфы по конкретному пользователю { if(empty($id_user)) { echo "Юзер не выбран"; } else { $query = mysql_query("SELECT * FROM sobstvenniki WHERE "); $array = mysql_fetch_array($query); return $array; } } ?>
Подвал

function CurrentUser($id_user) //Вывод инфы по конкретному пользователю
if(empty($id_user))
echo «Юзер не выбран»;
>
else
$query = mysql_query(«SELECT * FROM sobstvenniki WHERE «);
$array = mysql_fetch_array($query);
return $array;
>
>

?>
З.Ы. В вопрос сильно не вникал, если я что-то не допонял поправьте плз.

Источник

Displaying a online user list using PHP

Hello everyone so I have partly working code. However I also have issues with it. But let me start off to tell you what I am trying to achieve. On my site I have a login facility which sends all users to a home page once and only if the user is logged in. I would like to display who else is logged in on my page showing there usernames in a list.

My codes current ability.

Okay so i can code it to add the users on joining the site to a database and also then display each user on that database into a div which is great as it displays everyone online.

Where the problem stands.

However, when a user leaves the page there username needs to be deleted from the online user database. In the past I would simply do this with a on window close option in java. But due to Google safari and Firefox no longer supporting this option I am forced to find another way of doing it.

What the code bellow does.

So the code bellow loads an interval so after so much time it will repeat the code within it. The code first adds the user to the database, it then displays the user in a list on the site and then it deletes the user from the database just in case they are to then go offline.

Where the problem stands.

This all being very well for one user, but once a second user gets involved there intervals fall at a different time. So I have deleted the user from the database after it shows the list but then it means the second user displays the database and the first user isn’t in it as they are deleted. This also causes a problem should a user go offline half way though the function happening, then it leaves them in the database and doesn’t get around to deleting them out of the database.

The following is the java code.

 $(document).ready(function() < var user_name = ""; setInterval(function() < $.post('onlineusers.php', < name: user_name, action:"joined" >); $.post("onlineusers.php", < action2: "list" >, function(data)< $('#listusers').html(data); >); $.post("onlineusers.php", < nameleft: user_name, action3:"left" >, function(data)< $('#errorrreport').html(data); >); >, 5000); >): 

Now there is the PHP document code

 //------------User joined page put into database -------- if( $_REQUEST["name"]) < $user_name = $_REQUEST['name']; >; if( $_REQUEST["action"]) < $action = $_REQUEST['action']; >; if ($action == 'joined') < user_joined($user_name); >; //-----------Listing online users within page ------------- if( $_REQUEST["action2"]) < $action2 = $_REQUEST['action2']; >; if($action2 == 'list') < foreach (user_list() as $user)< echo $user . "
"; >; >; //------------ User left delete from the database ------------ if( $_REQUEST["nameleft"]) < $user_nameleft = $_REQUEST['nameleft']; >; if( $_REQUEST["action3"])< $action3 = $_REQUEST['action3']; >; if($action3 == 'left') < user_left($user_nameleft); >; //------ Functions what to do. -------- function user_joined($user_name) < $user_name = mysql_real_escape_string(htmlentities($user_name)); mysql_query("INSERT INTO users (user_name)VALUES('$user_name')") or die("this didn't happen"); >function user_left($user_nameleft) < $user_name = mysql_real_escape_string(($user_nameleft)); $query = mysql_query("DELETE FROM users WHERE user_name = '$user_nameleft'")or die("failed to delete from table"); >function user_list() < $user_list = array(); $users_query = mysql_query("SELECT user_name FROM users") or die ("Unable to collect userlist"); while ($users_row = mysql_fetch_assoc($users_query))< $user_list[] = $users_row['user_name']; >return $user_list; >

The above code

Sorry it is slightly messy due to my recoding of it so many times in order to get it to work. I would appreciate it if anyone could give me any help towards getting this to work. Now if it’s not just simple off the code from above I can add the user to the online user database on entrance to the page and get it to list who is online frequently from listing the users on the database.

The real issue and where i am asking for your help with please.

However, if you have any idea on the code for the following that would be great. The user leaving the page is where the problem comes up. I need some method of checking the user is still active and if they are not then deleting them from the database so when the list then refreshes the user is no longer on the list. Such as pinging something until it ends up with no response and deletes the user from the database. As my current code has problems with multiple users and synchronization of the interval function it needs to take that into account different users will see the list refresh at different times. P.S. I have also looked at using $SESSION however i am still not sure on how to make this work with checking for offline users and then deleting them from the database this might be a method of doing it. Thank you, I hope there is enough information to go on.

Источник

Оцените статью