简体   繁体   中英

php PDO query doesn't work with hash_hmac()

I just read about hash_hmac() function, i tried to use it for storing password in DB :

<?php
$dsn = 'mysql:host=localhost;dbname=test';
$username = 'root';
$pass = 'password';

$conn = new PDO($dsn, $username, $pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->setAttribute(PDO::ATTR_PERSISTENT, TRUE);

$password = '123MonyeTSahuR456';

    $p = addslashes(hash_hmac('sha256', $password, '13#slP3mK;"dA$@m', TRUE));    

    $query = "INSERT INTO table_one VALUES(NULL, :password)";

try
{
    $pdo = $conn->prepare($query);
    $pdo->bindValue(':password', $p);

    if($pdo->execute())
    {
        echo $pdo->rowCount();
    }
    else
    {
        echo 'fail';    
    }
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

the DB details:

id INT NOT NULL AUTO INCREMENT
password VARBINARY(32) NOT NULL

after that i manually check my DB and found a row with BLOB(i never work/use with VARBINARY or BLOB data type before, so this is my first time)

so there's 1 row exist and i try to select the row :

    $query = "SELECT * FROM table_one WHERE password = :password";

the value returned by pdo->rowCount() is 0 , it doesn't find the password im looking for, anyone can tell me why? how to fix this?

another details :

PHP version : 5.4

OS : Ubuntu 12.04 64 bit thanks

SOLVED

by using PDO::PARAM_LOB in the query, it works..

1 more thing in my mind is :

  • should i use addslashes() before the query? i just follow the tutorial for the addslashes() thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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