PHP 从数据库中选择用户名等于 X 的字段 [英] PHP Select fields from database where username equals X
问题描述
我在 PHP 中从用户名等于 $myusername 的数据库中选择信息时遇到问题
im having problems in PHP with selecting Infomation from a database where username is equal to $myusername
我可以使用会话将用户名从登录页面回显到登录页面.
I can get it to echo the username using sessions from the login page to the logged in page.
但我希望能够从该数据库中选择诸如bio"和email"之类的内容,并将它们放入名为 $bio 和 $email 的变量中,以便我可以回显它们.
But I want to be able to select things like 'bio' and 'email' from that database and put them into variables called $bio and $email so i can echo them.
这是数据库的样子:
有什么想法吗?:/
推荐答案
你应该连接到你的数据库,然后像这样获取行:
You should connect to your database and then fetch the row like this:
// DATABASE INFORMATION
$server = 'localhost';
$database = 'DATABASE';
$dbuser = 'DATABASE_USERNAME';
$dbpassword = 'DATABASE_PASSWORD';
//CONNECT TO DATABASE
$connect = mysql_connect("$server", "$dbuser", "$dbpassword")
OR die(mysql_error());
mysql_select_db("$database", $connect);
//ALWAYS ESCAPE STRINGS IF YOU HAVE RECEIVED THEM FROM USERS
$safe_username = mysql_real_escape_string($X);
//FIND AND GET THE ROW
$getit = mysql_query("SELECT * FROM table_name WHERE username='$safe_username'", $connect);
$row = mysql_fetch_array($getit);
//YOUR NEEDED VALUES
$bio = $row['bio'];
$email = $row['email'];
注 1:
不要使用纯文本作为密码,总是用盐来散列密码
Note 1:
Dont Use Plain Text for Passwords, Always hash the passwords with a salt
我使用 MYSQL_QUERY 作为您的代码,因为我不知道 PDO 或 Mysqli,在 MYSQL 中转义已经足够了,但请考虑使用 PDO 或 Mysqli,因为我不知道它们我无法为您编写代码
I used MYSQL_QUERY for your code because i don't know PDO or Mysqli, Escaping in MYSQL is good enought but Consider Using PDO or Mysqli , as i don't know them i can't write the code with them for you
这篇关于PHP 从数据库中选择用户名等于 X 的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!