从 php md5 字符串检查 MySql 中的字段

发布于 2024-08-11 07:38:29 字数 818 浏览 4 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青萝楚歌 2024-08-18 07:38:29

我不会混合&匹配MD5功能。将任何 md5 函数视为单向街可能更简单。所以修改为:

$str_concat = $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)) = MD5('$str_concat') LIMIT 1;";

或者为了简单起见,让sql完全匹配。

// Skip the php concatenation.

// 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)) = MD5(CONCAT('$str_a','$str_b')) LIMIT 1;";

I wouldn't mix & match MD5 functions. Probably simpler to consider any md5 function a one-way street. So modify it to be:

$str_concat = $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)) = MD5('$str_concat') LIMIT 1;";

Or just make the sql match exactly, for simplicity.

// Skip the php concatenation.

// 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)) = MD5(CONCAT('$str_a','$str_b')) LIMIT 1;";
鸢与 2024-08-18 07:38:29

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 in MySQL would return a different hash then the MD5 function in PHP is if the character set in MySQL is different.

忘羡 2024-08-18 07:38:29

尝试运行以下查询:

SELECT  MD5(CONCAT(first_col,second_col))
FROM    mytable
WHERE   first_col = 'hello'
        AND second_col = 'world'

并确保它返回 fc5e038d38a57032085441e7fe7010b0

还要检查 PHPMySQL 返回的 MD5 的大小写代码>匹配。

Try running the following query:

SELECT  MD5(CONCAT(first_col,second_col))
FROM    mytable
WHERE   first_col = 'hello'
        AND second_col = 'world'

and make sure it returns fc5e038d38a57032085441e7fe7010b0

Also check that the case of MD5 returned by PHP and MySQL match.

ι不睡觉的鱼゛ 2024-08-18 07:38:29

您的 $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.

忘羡 2024-08-18 07:38:29

** 我发现的唯一解决方案是使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文