PHP 从数据库中选择用户名等于 X 的字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17381779/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:47:22  来源:igfitidea点击:

PHP Select fields from database where username equals X

phpsqlvariablesselectwhere

提问by user2245512

im having problems in PHP with selecting Infomation from a database where username is equal to $myusername

我在 PHP 中从用户名等于 $myusername 的数据库中选择信息时遇到问题

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:

这是数据库的样子:

This is what the database looks like:

这是数据库的样子:

Any ideas?:/

有任何想法吗?:/

采纳答案by Vladimir

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'];

Note 1:

注 1:

Dont Use Plain Text for Passwords, Always hash the passwords with a salt

不要使用纯文本作为密码,总是用盐来散列密码

Note 2:

笔记2:

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,因为我不知道它们我无法为您编写代码

回答by acedanger

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 $linkvariable when creating (preparing) and executing your SQL scripts.

$link在创建(准备)和执行 SQL 脚本时使用该变量。

$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), $webIdand $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"];
}

回答by Sabashan Ragavan

From the picture I can see you are using phpMyAdmin - a tool used to handle MySQL databases. You first must make a connection to the MySql server and then select a database to work with. This is shown how below:

从图片中我可以看到您正在使用 phpMyAdmin - 一种用于处理 MySQL 数据库的工具。您首先必须连接到 MySql 服务器,然后选择要使用的数据库。如下所示:

<?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. So it might help to check it out as well.

当我回答你的问题时,我参考了这个网站:http: //webcheatsheet.com/PHP/connect_mysql_database.php。因此,检查它也可能会有所帮助。