从数据库中检索特定值php

时间:2016-02-26 17:52:05

标签: php html mysql database

我有一个大学项目,用户可以注册并登录网站。我已将其设置为在注册用户时输入用户名,电子邮件,密码,确认密码,地址和电话号码。注册后,他们会被分配一个随机字符串值,让我们称之为$ randomString。

所以我的数据库是login_db,它包含表用户。表格标题是用户名,电子邮件,密码,地址,电话号码,randomstring。

用户可以注册运行正常,他们的详细信息被添加到数据库中,用户可以登录。当他们登录时,他们被重定向到admin.php。此页面显示为空白。

还记得$ randomString人被分配了吗?我需要进入数据库检索$ randomString并显示在admin.php。

这是我到目前为止所尝试过的,我只能显示所有用户随机字符串,但我只需要登录用户randomString。所以我假设我需要经历的步骤是 1.检查IF用户是否已登录。可能----> if(isset($ _ SESSION [' email'])????
2.检查用户登录的内容。???? 3.检索登录用户randomString。 ????
4.显示随机字符串。也许----> echo $ result ???

我绝对不知道如何做到这一点,所以所有的帮助都表示赞赏。

$connection = mysqli_connect('localhost', 'root', '', 'login_db');

if($connection) {
echo "we are connected";
} else {
die("Database connection failed");
}


?>


<div class="jumbotron">
<h1 class="text-center"><?php

$query = "SELECT randomString FROM users";

$result = mysqli_query($connection, $query);

while ($row = mysqli_fetch_row($result)) {

print_r($row);

}

?></h1>
</div>

2 个答案:

答案 0 :(得分:0)

让我解释一下你的问题:

  

检查IF用户是否已登录

当用户登录系统时,您需要在会话中设置一些值,以便您可以检查每个新请求中的会话值,就像您在可能中所说的那样。即if(isset($_SESSION['email'])

  
    

检查用户是否已登录。????

  

如果您想知道哪个用户已登录,那么您还必须设置所有用户信息  您在会话中的(id,用户名/名字或姓氏等)与您保存的相同 会话中的用户电子邮件

  
    

检索登录用户randomString

  

要检索登录用户randomString,您可以使用用户首次登录时保留在会话中的用户信息来查询数据库。我是。

Select randromString from users_table where id='$_SESSION['id']'; //i had suppose you had kept id of logged in user in session here . 
  

显示随机字符串

要显示随机字符串,现在是,您只需使用上面的查询回显您从数据库中检索的$randomString的值。

答案 1 :(得分:0)

首先创建连接

<?php
$hostname_localhost = "localhost";
$database_localhost = "databasename";
$username_localhost = "root";
$password_localhost = "";
$localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

其次从登录用户的数据库中选择您想要的内容

<?php
$colname_record = "-1";
if (isset($_SESSION['email'])) {
  $colname_record = $_SESSION['email'];
}
mysql_select_db($database_localhost, $localhost);
$query_record = "SELECT userID, randomString, email FROM users WHERE email='$colname_record'";
$record = mysql_query($query_record, $localhost) or die(mysql_error());
$row_record = mysql_fetch_assoc($record);
$totalRows_record = mysql_num_rows($record);
?>

第三步获取登录用户的变量并回显它

<?php
$randomstring = $row_record['randomString'];
?>

并显示它

<?php
echo $randomstring;
?>
相关问题