php PHP获取特定用户的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17075424/
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
Get the data of specific user in PHP
提问by Swift Learner
Now I have created a login form with a session, what I need now that when the user login with his username and password, get his data such as name, about etc.. and put it in the welcome page.
现在我已经创建了一个带有会话的登录表单,我现在需要的是,当用户使用他的用户名和密码登录时,获取他的数据,例如姓名,关于等。并将其放入欢迎页面。
Currently I have created this code but this code get all users data,
目前我已经创建了这段代码,但这段代码获取了所有用户数据,
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("usersdata") or die(mysql_error());
$data = mysql_query("SELECT * FROM userid")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['Name'] . "</td> ";
Print "<th>Username:</th> <td>".$info['Email'] . " </td></tr>";
}
Print "</table>";
?>
I hope to find a way to do that. :D
我希望找到一种方法来做到这一点。:D
采纳答案by Omar Massad
Since you already created a login form with session then you get the data for the current logged in user by doing this:
由于您已经使用会话创建了登录表单,因此您可以通过执行以下操作获取当前登录用户的数据:
$_SESSION['userid']
: Should be filled in the login page.
$_SESSION['userid'] = $id
$_SESSION['userid']
: 应该在登录页面填写。
$_SESSION['userid'] = $id
Learn more about the sessions: PHP Sessions W3schools
了解有关课程的更多信息:PHP Sessions W3schools
And then:
进而:
$query= mysql_query("SELECT * FROM `userid` WHERE `id` = '".$_SESSION['userid']."' ")or die(mysql_error());
$arr = mysql_fetch_array($query);
$num = mysql_numrows($query); //this will count the rows (if exists)
HTML
HTML
<html>
//...
<?php if($num > 0){ ?>
<table border="1" cellpadding="3">
<tr><td colspan="2" align="center">Your Info</td></tr>
<tr>
<td>Name: <?php echo $arr['Name']; ?></td>
</tr>
<tr>
<td>Email: <?php echo $arr['Email']; ?></td>
</tr>
</table>
<?php }else{ ?>
User not found.
<?php } ?>
//...
</html>
回答by Jacob S
Although you should use the mysqli_ extension, rather than mysql_, you would want something like:
尽管您应该使用 mysqli_ 扩展名,而不是 mysql_,但您可能需要如下内容:
$result = mysql_query("SELECT * FROM userid WHERE username = '" . $username . "'")
or die(mysql_error());
if(mysql_num_rows($result) == 1) {
//Found the user
$row = mysql_fetch_array($result);
//Results can be accessed like $row['username'] and $row['Email']
} else {
//Too few or too many records were found
}
Note: I've used username='$username' as an example. It would be best to track the user's ID from the login process as the ID refers to a specific row.
注意:我以 username='$username' 为例。最好从登录过程中跟踪用户的 ID,因为 ID 指的是特定行。
回答by Michael
Your example code retrieves all users from the database and loops through the data using a while
loop.
您的示例代码从数据库中检索所有用户并使用循环遍历数据while
。
To get the user that has logged in you need to change your query that fetches the data. I'm assuming you have a primary key in your table and know the id because the user already logged in.
要获取已登录的用户,您需要更改获取数据的查询。我假设您的表中有一个主键并且知道 id,因为用户已经登录。
$data = mysql_query("SELECT * FROM userid WHERE id={$userid}");
$info = mysql_fetch_array( $data );
echo $info['Name'];
$info
will now contain all the user info for 1 user, you need to fill $userid
with the actual id from the user that is logged in.
$info
现在将包含 1 个用户的所有用户信息,您需要填写$userid
登录用户的实际 ID。
回答by amaster
$data = mysql_query("SELECT * FROM userid")
$data = mysql_query("SELECT * FROM userid")
Should be
应该
$data = mysql_query("SELECT * FROM userid WHERE Name='$selectedName'")
Of course you need to define $selectedName
当然你需要定义 $selectedName
I also recommend you read http://dev.mysql.com/doc/refman/5.0/en/select.htmlto learn about some fundamentals.
我还建议您阅读http://dev.mysql.com/doc/refman/5.0/en/select.html以了解一些基础知识。