[英]PHP Select fields from database where username equals X
im having problems in PHP with selecting Infomation from a database where username is equal to $myusername 我在PHP中遇到问题,从用户名等于$ myusername的数据库中选择Infomation
I can get it to echo the username using sessions from the login page to the logged in page. 我可以使用从登录页面到登录页面的会话来回显用户名。
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. 但是我希望能够从该数据库中选择诸如“ bio”和“ email”之类的东西,并将它们放入名为$ bio和$ email的变量中,以便我可以回显它们。
This is what the database looks like: 这是数据库的样子:
Any ideas?:/ 有任何想法吗?:/
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'];
Dont Use Plain Text for Passwords, Always hash the passwords with a salt 不要使用纯文本作为密码,请始终用盐对密码进行哈希处理
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 我将MYSQL_QUERY用于您的代码,因为我不知道PDO或Mysqli,在MYSQL中转义就足够了,但是考虑使用PDO或Mysqli,因为我不知道它们,我无法为他们编写代码
Simplistic PDO examples. 简单的PDO示例。
Create a connection to the database. 创建与数据库的连接。
$link = new PDO("mysql:host=$db_server;dbname=$db_name", $db_user, $db_pw, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Use the $link
variable when creating (preparing) and executing your SQL scripts. 在创建(准备)和执行SQL脚本时,请使用
$link
变量。
$stmt = $link->prepare('insert into `history` (`user_id`) values(:userId)');
$stmt->execute(array(':userId' => $userId));
Code below will read data. 下面的代码将读取数据。 Note that this code is only expecting one record (with 2 data elements) to be returned, so I'm storing whatever is returned into a single variable (per data element),
$webId
and $deviceId
. 请注意,此代码仅期望返回一条记录(包含2个数据元素),因此我将返回的内容存储到单个变量(每个数据元素),
$webId
和$deviceId
。
$stmt = $link->prepare('select `web_id`, `device_id` from `history` where `user_id` = :userId');
$stmt->execute(array(':userId' => $userId));
while($row = $stmt->fetch()) {
$webId = $row["web_id"];
$deviceId = $row["device_id"];
}
From the picture I can see you are using phpMyAdmin - a tool used to handle MySQL databases. 从图片中我可以看到您正在使用phpMyAdmin-一种用于处理MySQL数据库的工具。 You first must make a connection to the MySql server and then select a database to work with.
您首先必须建立与MySql服务器的连接,然后选择要使用的数据库。 This is shown how below:
如下所示:
<?php
$username = "your_name"; //Change to your server's username
$password = "your_password"; //Change to your server's password
$database = "your_database" //Change to your database name
$hostname = "localhost"; // Change to the location of your server (this will prolly be the same for you I believe tho
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db($database, $dbhandle)
or die("Could not select examples");
?>
Then you can write something like this: 然后,您可以编写如下内容:
<?php
$bio = mysql_query("SELECT bio FROM *your_database_table_name* WHERE username='bob' AND id=1");
?>
and 和
<?php
$email = mysql_query("SELECT email FROM *your_database_table_name* WHERE username='bob' AND id=1");
?>
Where *your_database_table_name* is the table in the database you selected which you are trying to query. 其中* your_database_table_name *是您要查询的所选数据库中的表。
When I was answering your question, I was referencing this site: http://webcheatsheet.com/PHP/connect_mysql_database.php . 当我回答您的问题时,我指的是这个站点: http : //webcheatsheet.com/PHP/connect_mysql_database.php 。 So it might help to check it out as well.
因此,也可能有助于检查出来。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.