php 在php中从Mysql获取当前用户ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13533271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:37:52  来源:igfitidea点击:

Get Current User ID from Mysql in php

phpmysqllogin

提问by Ozan

I create a Users Database and users have username, passwordetc. And each one has a unique auto_incrementid. I need this; For example if I login to the website it should be $id= my_id, if you login to the website it should be $id= your_id. Can you help please. Thanks.

我创建了一个用户数据库,用户有usernamepassword等等。每个人都有一个唯一的auto_incrementid。我需要这个; 例如,如果我登录该网站,它应该是$id= my_id,如果您登录该网站,它应该是$id= your_id。你能帮忙吗?谢谢。

I tried;

我试过;

$result = mysql_query("select id from Users where username ='".$_SESSION['username']."'");

$row = mysql_fetch_array($result);

$row = mysql_fetch_array($result);

$id = $row['id']; 

采纳答案by MrCode

In your login code (when you define $_SESSION['username']) you should store the user ID in the session, with any other user related data that you need.

在您的登录代码中(当您定义时$_SESSION['username']),您应该将用户 ID 与您需要的任何其他用户相关数据一起存储在会话中。

回答by cssyphus

Two users should not have the same username, even if they have the different user_ids.

两个用户不应具有相同的用户名,即使他们具有不同的 user_id。

You need some background help on creating your registration/login system. This would be a very good resource and would help you a lot: phpacademy.org Login/Registration Tutorial

您需要一些有关创建注册/登录系统的背景帮助。这将是一个非常好的资源,会对您有很大帮助:phpacademy.org 登录/注册教程

Please remember to upvote any posts that are helpful to you, and to choose an answer for your question when you are satisfied.

请记得为任何对您有帮助的帖子点赞,并在您满意时为您的问题选择一个答案。

回答by mohan.gade

add id feild in ur login check query . if login successful set that data in session or return to the login function . U get the id .

在您的登录检查查询中添加 id feild。如果登录成功,则在会话中设置该数据或返回登录功能。你得到了 id 。

回答by Pankaj Khairnar

Also store user idin to the $_SESSIONarray so that your user id will accessible in each request, use your query will look like

还将用户 id存储到$_SESSION数组中,以便您的用户 id 可以在每个请求中访问,使用您的查询看起来像

 $result = mysql_query("select username from Users where id ='".$_SESSION['id']."'");