-1

我正在尝试在 MySQL 中使用 AES_DECRYPT 来解密成功加密的 SSN。在输出中,我得到了“数组”这个词,而不是来自该字段的实际数据。我的 PHP 和 MySQL 知识有点生疏,所以我确信这是我忽略的一些愚蠢的事情。任何帮助,将不胜感激。

输出:
verify_name other_names ssn dob
:测试:测试:数组:测试

代码:
$key="88b871WZ3SntWK67rN3l2J1SvMqsOjyk";
$SQLstring = "SELECT * FROM applications";
$QueryResult = @mysql_query($SQLstring, $conn) or die("Query Problem - "
. mysql_error($conn) . " - Error Number - "
. mysql_errno($conn));
echo "verify_name other_names ssn dob";

$num_result = mysql_num_rows($QueryResult);
for ($i = 0; $i < $num_result; $i++)

{
$row = mysql_fetch_array($QueryResult);
$SQLstring2 = "SELECT AES_DECRYPT(ssn,'$key') FROM applications WHERE name='" . $row["name"] . "'";
$QueryResult2 = @mysql_query($SQLstring2, $conn) or die("Query Problem - "
. mysql_error($conn) . " - Error Number - " . mysql_errno($conn));
$num_result2 = mysql_num_rows($QueryResult2);
for ($j = 0; $j < $num_result; $j++){
$ssndecrypt = mysql_fetch_array($QueryResult2);

echo $ssndecrypt[0];
} echo $row["verify_name"];
echo $row["other_names"];
echo $ssndecrypt;
echo $row["dob"];

4

1 回答 1

0

这是因为您将结果作为数组获取。

$ssndecrypt = mysql_fetch_array($QueryResult2);
...
echo $ssndecrypt;

它正在回响Array,因为您从不重新分配$ssndecrypt变量。

然而,问题的核心似乎是您不必要地使查询复杂化。当您可以这样做时,没有理由两次查询表:

SELECT verify_name,
       other_names,
       dob,
       AES_DECRYPT(ssn,'$key') AS ssn
  FROM applications

这大大简化了代码:

$stmt = "SELECT verify_name, other_names, dob, AES_DECRYPT(ssn,'$key') AS ssn FROM applications";
$result = @mysql_query($stmt, $conn) or die("Query Problem - " . mysql_error($conn) . " - Error Number - " . mysql_errno($conn));
echo "verify_name other_names ssn dob";
while ($row = mysql_fetch_assoc($result))
{
    echo $row["verify_name"];
    echo $row["other_names"];
    echo $row["ssn"];
    echo $row["dob"];
}

但是,理想情况下,您应该使用PDO而不是mysql_*函数:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=mydb', $user, $pass);
    foreach($dbh->query("SELECT verify_name, other_names, dob, AES_DECRYPT(ssn,'$key') AS ssn FROM applications") as $row) {
        echo $row["verify_name"];
        echo $row["other_names"];
        echo $row["ssn"];
        echo $row["dob"];
    }
    $dbh = null;
} catch (PDOException $e) { die("ERROR: " . $e->getMessage()); }
于 2012-12-04T19:40:57.903 回答