I often find myself quickly needing the 1-way encrypted value (hash) of a string. Most often, it’s to securely store an individual password into a database or script. Rather than temporarily hard-coding the plain text to see what PHP will generate, I wrote a little reusable script to generate hash values. Anyone who might find it useful can find it here. If you’re paranoid about security, you can find a secure version here, although you may get a warning about my self-signed certificate.
Archive for the ‘Programming’ category
Quick Hash Generator
December 2nd, 2008Get a Random Row from Database
November 29th, 2008I had an app the other day that required a single random row to be displayed from the database. It’s an easy task, but I tripped on it for a second. Here was my impulse:
//Get the total number of rows from the MySQL database $query = "SELECT COUNT(*) as total FROM table"; $result = mysql_query( $query); $rows = mysql_fetch_assoc( $result ); //Let PHP choose a random row //pick a number between 1 & the total $row = rand(1, $rows['total']) //Now get that row $query = "SELECT * FROM table LIMIT($row, 1)"; $result = mysql_query( $query);
After typing that out, I realized that I had take the long way round the problem. Very similar to taking the Misty Mountain Path rather than the Mines of Moria. Here’s a better way:
//Get a random row from the database $query = "SELECT * FROM table ORDER BY RAND() LIMIT 1"; $result = mysql_query( $query );
Much better. Gimley the dwarf would be proud












