从 php md5 字符串检查 MySql 中的字段
我正在尝试通过 md5 函数从我的 php 页面验证 mysql 上的一对数据列。
我已经用 php md5 函数加密了字符串 "helloworld" ,并尝试将其与 MYSQL MD5 函数进行比较,但它不起作用。
我这样做是因为在数据库中有一对字符串 "hello" 和 "world" 需要与我的 php 字符串进行比较,所以:
<?php
$str_a = "hello";
$str_b = "world";
$str_encrypted = md5 ($str_a.$str_b);
// note "first_col" is "hello" and "second_col" is "world"
$sql = "UPDATE `my_table` SET `checked_col` = '1' WHERE MD5(CONCAT(first_col,second_col)) = '$str_encrypted' LIMIT 1;";
$res = mysql_query ($sql) or die (mysql_error());
($res) ? print "true" : print "false";
?>
此代码返回 false ,并且数据库不会更新列检查列,但不会返回mysql_error问题。
php 中的 md5 能否生成与 MYSQL 不同的 MD5 ?
朋友编写的类似代码在同一服务器上工作,但我没有太多经验来看出差异在哪里,
有人可以解释我错在哪里吗?
谢谢!
I'm trying to validate a pair of data columns on mysql from my php page across md5 function.
I've encrypted the string "helloworld" with php md5 function and attempted to compare it with MYSQL MD5 function but it won't work.
I do this because in the database there is a pair of strings "hello" and "world" needs to be compared with my php string, so:
<?php
$str_a = "hello";
$str_b = "world";
$str_encrypted = md5 ($str_a.$str_b);
// note "first_col" is "hello" and "second_col" is "world"
$sql = "UPDATE `my_table` SET `checked_col` = '1' WHERE MD5(CONCAT(first_col,second_col)) = '$str_encrypted' LIMIT 1;";
$res = mysql_query ($sql) or die (mysql_error());
($res) ? print "true" : print "false";
?>
This code return me false, and the database don't UPDATE the column checked column, but not mysql_error problems are returned.
Could the md5 from php generate a different MD5 from MYSQL?
a similar code written by a friend worked in the same server, but i don't have to much experience to see where is the difference
can someone explain me where I'm wrong?
thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不会混合&匹配MD5功能。将任何 md5 函数视为单向街可能更简单。所以修改为:
或者为了简单起见,让sql完全匹配。
I wouldn't mix & match MD5 functions. Probably simpler to consider any md5 function a one-way street. So modify it to be:
Or just make the sql match exactly, for simplicity.
MySQL
中的MD5
返回与PHP
中的MD5
函数不同的哈希值的唯一方法是,如果 <MySQL
中的 href="http://dev.mysql.com/doc/refman/5.1/en/charset.html" rel="nofollow noreferrer">字符集 不同。The only way that
MD5
inMySQL
would return a different hash then theMD5
function inPHP
is if the character set inMySQL
is different.尝试运行以下查询:
并确保它返回
fc5e038d38a57032085441e7fe7010b0
还要检查
PHP
和MySQL
返回的MD5
的大小写代码>匹配。Try running the following query:
and make sure it returns
fc5e038d38a57032085441e7fe7010b0
Also check that the case of
MD5
returned byPHP
andMySQL
match.您的 $res 很可能是假的,因为没有什么可更新的。如果您之前运行过 SQL 命令并更新了该行,则如果 check_col 仍为 1,则不会再次更新。由于 mysql_query 不更新任何内容,因此它将返回 false。
您可以包含一个 where 子句来忽略先前检查(验证)的行。
您可能不想使用更新的结果来确定验证是否成功,也许您想改用选择。
It is likely that your $res is false because there is nothing to update. If you have previously run your SQL command and updated the row, it will not update again if checked_col is still 1. Since mysql_query doesn't update anything, it will return false.
You could include a where clause to ignore previously checked (validated) rows.
You probably don't want to use the result of your update to determine whether validation was successful, perhaps you want to use a select instead.
** 我发现的唯一解决方案是使用 php 函数库的 md5 哈希部分插入密码(或更新表),这样当您将哈希与数据库进行比较时它就可以工作。所有其他将 php 的 md5 哈希与 mySQL 的 md5 函数进行匹配的尝试都会失败。
如果有人知道为什么会发生这种情况(我相信旧版本的 php 和 mysql 没有这个问题),我很想知道 - 谢谢,rick
** The only solution I found is to insert the password (or update the table) using the md5 hash part of php's function library, that way when you compare the hash to the database it works. All other attempts to match an md5 hash from php with the md5 function of mySQL fail.
If anybody knows why (I believe older versions of php and mysql dont have this problem) this happens, I would love to know - thanks, rick