So I have one main query that selects records from today. And if today's records are less than 3, then perform another query. This is how I'm currently doing it:
<?php
//Select today's records
$select = mysql_query("SELECT * FROM item_info
WHERE item_info.content_time
BETWEEN UNIX_TIMESTAMP( CURDATE( ) )
AND UNIX_TIMESTAMP( CURDATE( ) + INTERVAL 1
DAY )
ORDER BY item_info.item_id DESC
LIMIT 9 ");
//If today's records are less than 3, select the ones from yesterday
if(mysql_num_rows($select) < 3){
$select = mysql_query("SELECT * FROM item_info
WHERE item_info.content_time
BETWEEN UNIX_TIMESTAMP( CURDATE( ) + INTERVAL -1
DAY )
AND UNIX_TIMESTAMP( CURDATE( ) )
ORDER BY item_info.item_id DESC
LIMIT 9 ");
}
//Fetch $select
?>
My main concerns are:
-Is this a proper use of mysql_num_rows?
-Are there other ways to do this?
Usually there will be more than 3 records each day, so the subquery is just in case.
Yes there is another way to do it. Run your query to always select the top 9 records from yesterday and today. Modify your order by clause so that it looks like this:
order by item_info.content_time desc, item_info.item_id desc
That way you don't need to check the number of records returned.
However, this is slightly different logic. It will give a different answer than what you currently have if the number of records for today is between 3 and 9. If that's not ok, keep what you have.
By the way, you should verify that your current code does meet your requirements. Currently, if you get 1 or 2 records from today, you ignore them and use yesterday's data. That might not be what you had in mind.