'Writing SQL command to update a password in a part of a column

I am trying to write an sql command that enters a password inside a string.

The database entry that I need changed is in the users table preferences column and it looks like this: a:2:{s:11:"client_hash";s:16:"hashedpwdhere";i:0;b:0;} or

a:1:{s:4:"skin";s:5:"larry";s:11:"client_hash";s:16:"hashedpwdhere";}

All I need is to replace the "hashedpwdhere" with the new hashed password. That is always after s:16: no matter how many other things are in there.

Currently my SQL command looks like this:

UPDATE users SET preferences=ENCRYPT(%p,CONCAT(_utf8\'$5$\',RIGHT(MD5(RAND()),8),_utf8\'$\')) WHERE username=%u LIMIT 1

Unfortunately this erases everything in the preferences column and I have no clue how to write this to only insert it into the quotes after s:16:

Thank you for any help you can provide!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source