It's usually effective to take the task of getting data out of the database, preparing it for output, and displaying it into three separate tasks:
Getting data out of the database
Preparing it for output
Displaying it
.
Step 1.
Get the data out of the database. You could use "LIMIT $limit" to restrict the number of records retrieved to $limit. To guarantee that you only get a multiple of three, use "LIMIT 3*$limit" so that setting $limit to 4 will get you no more than 12 records (possibly less, if fewer match). Fetch all those records from the result and store them in an array.
Step 2.
Preparing it for output. Count how many you've got, and if it's not a multiple of three, drop one or two off the end (like, for example, if you get fourteen records then discard two to leave you with twelve). [man]array_slice[/man] can be used for that. The modulus operator '%' is also good. In fact, $records = array_slice($records, 0, -(count($records) % 3)) does the counting and trimming in one statement.
Then you can group the records in threes. [man]array_chunk[/man].
Step 3.
Iterate through the array you get back from array_chunk, using the usual alternating rows idea. Only instead of having one record, you've got an array of three records. Display all three and go on to the next iteration.
Step 3.