2015-11-01 145 views
-1

我有一个注册页面,其中有输入框,您可以在其中输入您的姓名,电子邮件地址和密码。提交该表单后,会有一个登录页面,在该页面中检查您是否在SELECT * FROM users WHERE name = '$_POST[name]' AND email = '[email]'的数据库中。这一切都很好,但是当你真正进入你的帐户的网站时,我想在顶部有一个消息,说'欢迎回来(数据库名称)。为此,我使用$name = mysql_query("SELECT first_name FROM users WHERE email = $email");,顶部有<?php echo $name ?>。这不会工作。为什么?从mysql数据库检索数据

+0

岗位多一点,这样我们就可以知道你是如何访问和显示数据 – Mubin

+0

你需要阅读,学习和理解正确的教程。 –

+0

你问了很多问题,但还没有接受任何答案。这个网站是双向的。如果你期望得到帮助,你应该通过接受他们的回答来奖励那些帮助你的人。阅读[接受答案如何工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)并开始回馈社区。 –

回答

3

“mysql_query”已被弃用,您现在要使用“mysqli_query”。看到这里:http://php.net/manual/en/function.mysql-query.php

要回答你的问题,执行查询创建一个数组,你需要通过从数组中提取后续。

如果在过程风格这样做,则最终的代码看起来应该是这样的:你的代码的

$link = mysqli_connect("localhost", "my_user", "my_password"); // connect to the database 
$query="SELECT first_name FROM users WHERE email = '".$email."'"; // create the query. Note the quotes arounds the email variable. 
$result=mysqli_query($link, $query); // run the query 
$row=mysqli_fetch_array($result); // fetch the array that is returned from the query 
$name=$row['first_name']; // assign the first_name field to the $name variable 

echo "Hello ".$name.","; // output the variable 
+1

欢迎来到Stackoverflow! –

+1

小修正...你不想将**密码**作为纯文本存储。用纯文本保存诸如用户名和电子邮件地址之类的数据应该没有问题。 –

+0

@MikeEverhart我以前读过一篇文章,建议以安全的方式存储任何个人身份信息(http://programmers.stackexchange.com/questions/271376/should-i-keep-email-addresses-as-plaintext-在数据库)。这是我的第一篇文章,我不想通过告诉某人对个人数据做一些不安全的事来摇摆船。但在阅读过这方面的内容后,我认为我过于谨慎了,我同意你的看法。我从我的答案中编辑了这个建议。谢谢! –