在PHP中获取特定用户的数据 [英] Get the data of specific user in PHP
问题描述
现在,我创建了一个会话的登录表单,我需要现在,当用户用他的用户名和密码登录,获取他的数据,如名称,大约等等,并把它放在欢迎页面。 p>
目前我已创建此代码,但此代码会获取所有用户的数据
<?php
mysql_connect(localhost,root,)或die(mysql_error
mysql_select_db(usersdata)或die(mysql_error());
$ data = mysql_query(SELECT * FROM userid)
或die(mysql_error());
打印< table border cellpadding = 3>;
while($ info = mysql_fetch_array($ data))
{
打印< tr>;
打印< th>名称:< / th>< td>。$ info ['Name']。 < / td>;
打印< th>用户名:< / th>< td>。$ info ['Email']。 < / td>< / tr>;
}
打印< / table>;
?>
我希望找到一种方法。 :D
由于您已经创建了一个具有会话的登录表单,因此您可以通过执行此操作获取当前登录用户的数据:
$ _ SESSION ['userid']
:应填写登录页面。
$ _ SESSION ['userid'] = $ id
详细了解会话: PHP会话W3schools
然后:
$ query = mysql_query(SELECT * FROM` userid` WHERE`id` ='。$ _ SESSION ['userid' ]。')或die(mysql_error());
$ arr = mysql_fetch_array($ query);
$ num = mysql_numrows($ query); //这将计算行(如果存在)
HTML p>
< html>
// ...
<?php if($ num> 0){?>
< table border =1cellpadding =3>
< tr>< td colspan =2align =center>您的信息< / td>< / tr>
< tr>
< td>名称:<?php echo $ arr ['Name']; ?>< / td>
< / tr>
< tr>
< td>电子邮件:<?php echo $ arr ['Email']; ?>< / td>
< / tr>
< / table>
<?php} else {?>
找不到用户。
<?php}?>
// ...
< / html>
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
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
Learn more about the sessions: 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>
//...
<?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>
这篇关于在PHP中获取特定用户的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!