Thursday, 7 July 2011

Getting Pseudo Random Records From A MySQL Database Table

Here’s a common programming problem: You have some records in a MySQL table. You want to randomly select a record, or just a few records, from that database; maybe, you want to pass some additional, specific criteria to narrow the candidate records somewhat.

For example, maybe you run a music Web site. You have a database table full of albums, and you want to randomly pick one as “album of the moment.” Perhaps you want to select from a category — rock, country, jazz, etc. Or maybe you want to get an assortment of albums from a specific artist, and display many records on a Web page.

Getting such “random” database records is easily done in MySQL (versions 3.23 and up) via a fairly standard SELECT statement, using ORDER BY, RAND and LIMIT.

(For the purpose of this tutorial, I’ll use the ZIP Code database I got for Getting All ZIP Codes In A Given Radius From A Known Point / ZIP Code Via PHP And MySQL, which I wrote some time ago.)

First, let’s simply select any old record from the table; that is, every record in the table is equally eligible for selection.

SELECT * FROM table_name ORDER BY RAND() LIMIT 1;

What this does is tell MySQL, “Toss all the records together in a pile [ORDER BY RAND()], then give me just one record from that pile [LIMIT 1].”

You can see this in action here: http://www.dougv.com/demo/mysql_random_records/index.php

Continue reading: Getting Pseudo Random Records From A MySQL Database Table »

Friday, 5 October 2007

Take That, Bandwidth Leeches

A recent trip through my hit reports revealed a startling fact: This site’s bandwidth is being heavily leeched.

Yes, I never thought it could happen to me, an innocent little development blog that posts an occasional hot babe. But then, it did happen; it seems many, many people like the image of

So, I have struck my revenge with mod_rewrite. With a couple lines of code, you can pretty much shut down hotlinking of images and other resources, period:

Continue reading: Take That, Bandwidth Leeches »

Saturday, 9 December 2006

Fixing A "Bad Minute" Error Message When Trying To Use Crontab With Certain Unix Text Editors

A helpful tidbit I ran across about using crontab.

If each of your cron jobs do not appear on a single line in the crontab text file, you’ll get an error like this:

/tmp/crontab.XXXXXX.crontab":1: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit?

This problem most often occurs because you’re using a text editor, such as pico, that fakes word wrapping by adding a newline when it reaches a certain column position.

Crontab delimits jobs with line breaks (newlines). Each job occupies one line. Therefore, if crontab sees anything other than an integer in the first column of a line, it throws the “bad minute” error, since the minute argument is the first one crontab encounters.

The simple fix is to go to delete the line breaks added by pico / your *nix editor. You can most easily do that by putting your cursor on the first character of each extra line, then hit the backspace key until that line is joined back up with the previous one, and repeating the process until your entire cron command is on one line.