0

Trying to do the SHA1 of a variable, I'm using this snip of code:

$prow=mysql_fetch_array(mysql_query("SELECT SHA1($password) AS SHA"));
var_dump($prow);

However, I'm always getting prow to be assigned as null, why is it?

16
  • 1
    You need a FROM tablename Commented Oct 15, 2013 at 15:55
  • 3
    You should not write this kind of all-in-one-line statement – it makes debugging harder (and we can see from your question that you have totally failed at debugging here). Commented Oct 15, 2013 at 15:56
  • 1
    @Fred-ii-: He's not selecting from a table. Commented Oct 15, 2013 at 16:03
  • 3
    @Fred-ii-: It's not a "rule". You only need a FROM if you are selecting from a table. SELECT SHA1('ABC') as SHA is a valid query. You are just working with literals, and not field names. Commented Oct 15, 2013 at 16:04
  • 3
    Chances are $password is a string, and doesn't have quotes around it. Try mysql_query("SELECT SHA1('$password') AS SHA"). P.S. Do not use the database just for string processing. Also NEVER assume your query succeeded. ALWAYS check mysql_error(). $sql = mysql_query(...); if(!$sql){ die(mysql_error()); } $row = mysql_fetch_array($sql);. Commented Oct 15, 2013 at 16:06

2 Answers 2

1

This works for me....

$password = 123; 
$query    = "SELECT SHA1($password) AS SHA"; 
$result   = mysql_query($query); 
$prow     = mysql_fetch_array($result);

print "<pre>";
print_r($prow);
print "</pre>";

and returns:

Array
(
    [0] => 40bd001563085fc35165329ea1ff5c5ecbdbbeef
    [SHA] => 40bd001563085fc35165329ea1ff5c5ecbdbbeef
)

try testing your database connection...?

edit -- use mysql_ping() to test your connection. if it returns false, that's your issue....

edit -- you need quotes around the string... i can't believe i missed that.

$query = "SELECT SHA1('$password') AS SHA"; 
Sign up to request clarification or add additional context in comments.

5 Comments

What if $password is a string, and not a number?
oh! then it fails for mysql_fetch_array because $result is not a valid resource. good job!
Why does the return value twices the result in number and tag based indexes?
if you look at the documentation for that function ($results->fetch_array()), it takes an optional parameter that says what the return format is... MYSQLI_ASSOC, MYSQLI_NUM, or MYSQLI_BOTH (and those are PHP constants). By default, it returns MYSQLI_BOTH which is both the associative array and numerically indexed array... so if you want just an associative array just call it like $results->fetch_array(MYSQLI_ASSOC).
OH! I did that comment for the mysqli_ function but I am going to leave that there because it's the same idea. This is documentation for mysql_fetch_array and it would look like this if you wanted just the associative array returned: mysql_fetch_array($result,MYSQL_ASSOC); They are slowly removing the deprecated mysql_ functions so you should try to use/learn themysqli_ or pdo classes.
1

Null is returned when an error occurs. Your call to mysql_query() is probably returning false.

If that's all you're doing with the database, why not use PHP's own sha1() function:

$sha1 = sha1($password);

(N.B. you should be using bcrypt or scrypt to hash passwords properly - SHA1 isn't adequate.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.