简体   繁体   English

使用PHP在MySQL中选择两个字段

[英]Selecting two field in MySQL with PHP

i'm relatively new to php and mysql and would like to know how to select two value in mysql with php. 我对php和mysql比较陌生,想知道如何在php中选择mysql中的两个值。

What i have is 我有的是

$query = sprintf("SELECT COUNT(id) FROM table WHERE UPPER(username) = UPPER('%s') AND password='%s'"...

in this case, i'm only selecting and count if the id exist and i use 在这种情况下,我只选择并计算ID是否存在,并且我使用

list($count) = mysql_fetch_row($result);
        if($count == 1)

and by using cookies, i would like to retrieve two value from the database, namely user (the user's name) and power (which has value of 1,2 or 3, indicating the menu they would be able to see) basically it is to differentiate if you're admin or normal user, but i wonder if i could do 并通过使用cookie,我想从数据库中检索两个值,即用户(用户名)和power(值分别为1,2或3,表示他们将能够看到的菜单),基本上是区分您是管理员还是普通用户,但我想知道是否可以

SELECT COUNT(id) AND power FROM table WHERE ...

is this possible? 这可能吗? or is there any other way? 或还有其他方法吗?

Please guide me, thanks. 请指导我,谢谢。

You will have to do something like this: 您将必须执行以下操作:

SELECT username, power FROM table WHERE UPPER(username) = UPPER('%s') AND password='%s'"...

So, if it gives you a result, it means the username and password match, and you will have the username and power of that row. 因此,如果它给您结果,则意味着用户名和密码匹配,您将拥有该行的用户名和功能。

Bye! 再见!

You could just do 你可以做

SELECT power FROM table WHERE ... LIMIT 1

and then: 接着:

$numRows = mysql_num_rows($result);
if($numRows == 1){
    $pow = mysql_fetch_row($result)[0];
}

(not tested, but should work I think :)) (未经测试,但我认为应该可以工作:))

EDIT: 编辑:

If you want to select more than one field: 如果要选择多个字段:

SELECT power, somethingelse, yetanotherfield FROM table ...

$row = mysql_fetch_row($result);
$pow = $row[0];
$stelse = $row[1];
...

Why do you need to count the number of 'id' fields? 为什么需要计算“ id”字段的数量?

If you want to just pull only two fields from a database, you should use 'SELECT id,power FROM table WHERE ...' 如果只想从数据库中提取两个字段,则应使用“ SELECT id,power FROM table WHERE ...”

AND is a logical operator that returns true iff all its arguments are true. AND是逻辑运算符, 如果其所有参数均为true,则返回true。 If you're talking about having more than one column in a result, simply separate them by commas: 如果您要讨论的结果中包含不止一列,只需用逗号将它们分开:

SELECT COUNT(id), power 
  FROM table
  WHERE ...

However, if power isn't functionally dependent on id and there are more than one row with a given id , you could get any of the power values (on DBMSs other than MySQL, you'd need to GROUP BY power for the query to even work). 但是,如果power功能上不依赖id且给定id超过一行,则可以获取任何power值(在MySQL以外的DBMS上,您需要GROUP BY电源才能查询到)甚至工作)。

Instead of enforcing uniqueness in PHP, declare a UNIQUE index on column username. 不是在PHP中强制唯一性,而是在列username上声明UNIQUE索引 Column id should be a primary key, which implies that it's unique. id应该是主键,这意味着它是唯一的。

CREATE TABLE users (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(64) UNIQUE NOT NULL,
    ...
);

Or, if the table already exists, 或者,如果该表已经存在,

CREATE UNIQUE INDEX username ON users (username);

Off Topic 无关

By calling UPPER on column username, you're preventing any index from being used, causing MySQL to have to scan the entire table to execute the query. 通过在列用户名上调用UPPER ,可以防止使用任何索引,从而导致MySQL必须扫描整个表才能执行查询。 If you want your user names to be case insensitive, convert them before storing them. 如果您希望用户名不区分大小写,请在存储前进行转换。 This is easily accomplished with triggers , which you can read about in the MySQL reference manual. 这可以通过触发器轻松完成,您可以在MySQL参考手册中阅读有关触发器的信息。

delimiter ;;
CREATE TRIGGER upcase_username_insert
  BEFORE INSERT
  ON TABLE users
FOR EACH ROW
  NEW.username=UPPER(NEW.username)
END;;
CREATE TRIGGER upcase_username_update
  BEFORE UPDATE
  ON TABLE users
FOR EACH ROW
  NEW.username=UPPER(NEW.username)
END;;
delimiter ;

While using sprintf to combine strings will work fine, it's not standard practice. 虽然使用sprintf组合字符串可以很好地工作,但这不是标准做法。 Variables are interpolated into double quoted strings, so you can simply write "SELECT ... WHERE " . 变量被插到双引号字符串中,因此您只需编写"SELECT ... WHERE "

Make sure you're storing hashed and salted passwords (using a cryptographically secure hash, which MD5 isn't, these days) rather than plain passwords. 确保您存储的是散列密码和加盐密码 (目前使用的是加密安全的哈希,而MD5并非如此),而不是普通密码。

Finally, but most importantly, depending where the values interpolated into the query come from and what other processing is done on them, your query could be vulnerable to SQL injection . 最后,但最重要的是,取决于插入到查询中的值的来源以及对它们进行的其他处理,查询可能容易受到SQL注入的攻击。 Use PDO and prepared statements instead (prepared statement parameters are invulnerable to SQL injection). 请改用PDO预处理语句 (预处理语句参数对SQL注入无害)。 Read " Writing MySQL Scripts with PHP and PDO " for a PDO tutorial. 阅读“ 使用PHP和PDO编写MySQL脚本 ”以获取PDO教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM