CodeIgniter Forums

Full Version: [Solved] Getting SHA1 value from MySql
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]VasilK[/eluser]
Hello!

I would like to know how I can use the SHA1 function from the MySQL. I am trying with this line:

Code:
$confirmation_hash = $this->db->query("SHA1('{$confirmation}')");

But I got this error: You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'SHA1

Could you please help me!

Thanks you!
Vasil

El Forum

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

Try this:
Code:
$confirmation_hash = $this->db->query("SELECT SHA1('{$confirmation}')");

Not too sure why you're using the database for this, though...

El Forum

[eluser]VasilK[/eluser]
Thank you for the warm welcome! Smile
[quote author="TheFuzzy0ne" date="1244745185"]
Try this:
Code:
$confirmation_hash = $this->db->query("SELECT SHA1('{$confirmation}')");
[/quote]

Yes, it's much better! Still, now I have problem to access the value. I tried with this:
Code:
$sample = $confirmation_hash->first_row('array');
But looks like the argument to access the filed in $sample is the value of the $confirmation itself... or maybe I did not understand it right! Could you please share with me the few lines how to get the value from this query. Thanks!

[quote author="TheFuzzy0ne" date="1244745185"]
Not too sure why you're using the database for this, though...[/quote]

Hm... I was being told that sometimes, the sha1 php function could be bit different from the MySQL SHA1. In this case, I use the MySQL SHA1 when saving in the database, so I need to create that one again.

Take care!
Vasil

El Forum

[eluser]deanf7[/eluser]
if you're trying to retrieve the actual password you can't. It's a one way hash that can't be decrypted. If a user forgets their password you'd have to reset it.

El Forum

[eluser]VasilK[/eluser]
[quote author="deanf7" date="1244749963"]if you're trying to retrieve the actual password you can't. It's a one way hash that can't be decrypted. If a user forgets their password you'd have to reset it.[/quote]

Yes, I know that!

What I am asking is: How I can get the SHA1 value from the result query?

El Forum

[eluser]Evil Wizard[/eluser]
Something like this should help
Code:
$confirmation_hash = $this->db->query("SELECT SHA1('{$confirmation}') AS `confirmed`");
$sample = $confirmation_hash->row(0)->confirmed;

El Forum

[eluser]VasilK[/eluser]
Done. Thank you all!

Looking forward of sharing with you here!
Vasil

El Forum

[eluser]Evil Wizard[/eluser]
Although if it is just the sha1 value of $confirmation that you want, I would be more inclined to use the php sha1() function, got to be less overhead than a DB round trip