PHP 从数据库中获取用户名,在页面上显示用户名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36966434/
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
PHP Get username from Database, display username on page
提问by NiceDevelopmentq
I am currently trying to get the username from my database into my webpage and display it. I am not having much luck, this is the code that I have so far. Should work, but the $username variable is undefined, not sure why.
我目前正在尝试从我的数据库中获取用户名到我的网页中并显示它。我运气不好,这是我迄今为止的代码。应该可以工作,但 $username 变量未定义,不知道为什么。
<?php
/* Database Information */
$server = '127.0.0.1';
$database = 'nicedevelopment';
$dbuser = 'root';
/* Database Connection */
$connect = mysql_connect("$server", "$dbuser")
OR die(mysql_error());
mysql_select_db("$database", $connect);
/* Escape String */
$username = mysql_real_escape_string($connect);
/* Find and get row*/
$getit = mysql_query("SELECT * FROM users WHERE username='$username'",$connect);
$row = mysql_fetch_array($getit);
$username = $row['username']
?>
<nav>
<div class="nav-wrapper">
<a href="#" class="brand-logo center"><?php echo $row['username']; ?></a>
This is my login page!
这是我的登录页面!
<?php
require('connect.php');
session_start();
// If form submitted, insert values into the database.
if (isset($_POST['email'])){
$email = $_POST['email'];
$password = $_POST['password'];
$email = stripslashes($email);
$email = mysql_real_escape_string($email);
$password = stripslashes($password);
$password = mysql_real_escape_string($password);
//Checking is user existing in the database or not
$query = "SELECT * FROM `users` WHERE email='$email' and password='".md5($password)."'";
$result = mysql_query($query) or die(mysql_error());
$rows = mysql_num_rows($result);
if($rows==1){
$_SESSION['email'] = $email;
header("Location: dashboard.php"); // Redirect user to index.php
}else{
echo "<div class='form'><h4>Email/password is incorrect.</h4><br/>Click here to <a href='index.php'>Login</a></div>";
}
}else{
?>
回答by The Codesee
Updated Answer
更新答案
Since the user is only able to login with their email, you won't be able to use a session
to display their username (unless you want to display their email!). Your best bet would be to use a query which selects the username from a table where the email equals the email they used to login with:
由于用户只能使用他们的电子邮件登录,您将无法使用 asession
显示他们的用户名(除非您想显示他们的电子邮件!)。最好的办法是使用查询从表中选择用户名,其中电子邮件等于他们用来登录的电子邮件:
<?php
$email = $_POST['email'];
$query = "SELECT * FROM users WHERE email='$email'"
$row = mysql_fetch_array($query);
$username = $row['username'];
echo $username;
?>
Original Answer
原答案
According to your query, you're trying to select a username from the database which is equal to $connect = mysql_connect("$server", "$dbuser")
- this doesn't make sense at-all.
根据您的查询,您试图从数据库中选择一个用户名,该用户名等于$connect = mysql_connect("$server", "$dbuser")
- 这根本没有意义。
You first want to log the user in, like this:
您首先要让用户登录,如下所示:
<?php
session_start();
if(isset($_POST['submit'])) {
$_SESSION['username'] = $_POST['username'];
}
<form method="post">
<input type="text" name="username">
<input type="password>
<input type="submit" name="submit">
</form>
To then display the name of the user, simply use these two lines of code:
要显示用户名,只需使用以下两行代码:
session_start();
echo $_SESSION['username];
Note: this is a very basic script. It doesn't prevent any forms of SQL Injection Attacks and doesn't validate the username and password - it's just an example to get you on the right track.
注意:这是一个非常基本的脚本。它不会阻止任何形式的 SQL 注入攻击,也不会验证用户名和密码——这只是让您走上正轨的一个例子。
回答by shen
Update
更新
Try this code, fill the password field and let me know about the output. Although it is not clear where is your $username variable coming from.
试试这个代码,填写密码字段并让我知道输出。虽然不清楚您的 $username 变量来自哪里。
Edit: I have seen that you updated your question, so I modified the code based on that. I also edited the code based on the feedback of the questioner.
编辑:我已经看到您更新了您的问题,因此我基于此修改了代码。我还根据提问者的反馈编辑了代码。
<?php
$dbservername = "127.0.0.1";
$dbusername = "root";
$dbpassword = "password";
$dbname = "nicedevelopment";
// Create connection
$conn = new mysqli($dbservername, $dbusername, $dbpassword, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$post_email = mysqli_real_escape_string($conn, $_POST['email']);
$sql = "SELECT * FROM users WHERE email='$post_email'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
$row = $result->fetch_assoc();
echo "username: " . $row['username'] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>