php MD5 密码的 SQL 更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8782356/
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
SQL Update of MD5 Passwords
提问by Jahed Hussain
I am new to PHP & SQL. I am trying to update a password from my database and I cant figure out the SQL statement so I done some research and came across this SQL statement:
我是 PHP 和 SQL 的新手。我正在尝试从我的数据库更新密码,但我无法弄清楚 SQL 语句,所以我做了一些研究并发现了这个 SQL 语句:
UPDATE `Users` SET password= passwordmd5 (password)
I then added a bit more to the code as follows:
然后我在代码中添加了更多内容,如下所示:
UPDATE `Users` SET password= tony123 MD5 (password) WHERE user_id = 55
I get the following error:
我收到以下错误:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'MD5 (password) WHERE user_id = 55' at line 1
What do I do?
我该怎么办?
回答by Sergio Tulentsev
Correct syntax would be this:
正确的语法是这样的:
UPDATE Users
SET password = MD5('tony123')
WHERE user_id = 55;
Or, if you were storing password in plain text and you want to convert them to hashes, do this:
或者,如果您以纯文本形式存储密码,并且想将它们转换为哈希值,请执行以下操作:
UPDATE Users
SET password = MD5(password);
回答by Jahed Hussain
this will work after you add db connection strings to your php file:
将 db 连接字符串添加到 php 文件后,这将起作用:
<?php
$password = 'tony123';
$passwordmd5 = md5($password);
$q = mysql_query("UPDATE `Users` SET password = '$passwordmd5' WHERE user_id = 55");
?>
php/mysql connection ref: http://php.net/manual/en/function.mysql-connect.php
php/mysql 连接参考:http: //php.net/manual/en/function.mysql-connect.php
回答by Imran Qamer
i have stored password as a text into db and when i converted it to MD5 password i used this query,
我已将密码作为文本存储到 db 中,当我将其转换为 MD5 密码时,我使用了此查询,
UPDATE tablename SET columname = MD5(columname);
in above query you can also use any method instead of MD5 by just replacing MD5.
在上面的查询中,您还可以通过替换 MD5 来使用任何方法来代替 MD5。