PHP ADODB - Check if Query Returns Any Results With Two Different Conditions

I have the same query filtered by two different conditions and want to check if it returns results, in the example below, I write two queries and use the PHP ADODB built in RecordCount function, but is there a more efficient way to do this with just one query ?


$query1 = "SELECT col1, col2, catid FROM table WHERE catid = 1";
$result1 = $db->execute($query1);
$totalRows_result1 = $result1->RecordCount();

$query2 = "SELECT col1, col2, catid FROM table WHERE catid = 2";
$result2 = $db->execute($query2);
$totalRows_result2 = $result2->RecordCount();

if ($totalRows_result1 < 1) { 
	echo 'sorry, category 1 has no items';
}

if ($totalRows_result2 < 1) { 
	echo 'sorry, category 2 has no items';
}

What is the overall goal of doing this? Do you just want to find if the catid value(s) exist or do you actually want to use the data if it is found and are those all the catid values or just two that you are interested in?

Edit: also, can there be more than one row with the same catid value?

Short-version: You are showing us what you are trying to make work, not what you are really trying to accomplish within the application.

Yes, there can be note than one row, I just want to echo a message if at the condition returns at least one row.

My question, is there are more efficient way to do this with a single query, rather than two queries?

Why? What is the high level overview of what you have going on? What if there are records, then what?

Just want to echo a msg if there are no rows and echo alternative message if at least one row

Sponsor our Newsletter | Privacy Policy | Terms of Service