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?
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";
$password is a string, and not a number?mysql_fetch_array because $result is not a valid resource. good job!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).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.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.)
FROM tablenameFROMif you are selecting from a table.SELECT SHA1('ABC') as SHAis a valid query. You are just working with literals, and not field names.$passwordis a string, and doesn't have quotes around it. Trymysql_query("SELECT SHA1('$password') AS SHA"). P.S. Do not use the database just for string processing. Also NEVER assume your query succeeded. ALWAYS checkmysql_error().$sql = mysql_query(...); if(!$sql){ die(mysql_error()); } $row = mysql_fetch_array($sql);.