If you do a straight query, the normal result is a recordset, which will have rows if the conditions are met, and nothing if the conditions are not met.
If you try to perform just an aggregate function like MAX() or MIN() for a condition that can't be met, the result set will be a null value.
If you try to perform just an aggregate function like COUNT() OR SUM() for a condition that can't be met, the result set will be zero.
These I knew.
The easiest way to understand how the query to behave is to test it in the database (phpmyadmin, sql server management studio, etc.). Try the query where the conditions are met, and when they're not, and see how the resultsets are returned.
Well, I did that, and I saw that the query returned "NULL" when there were no Comments, but my fear was how to handle things if things "blow up".
So, to frame this question better - although this is more of a programming question - how should my code handle things?
Using pseudo-code, I do this...
// Build query.
$q2 = "SELECT MAX(comment_no) AS lastCommentNo
WHERE article_id = ?";
// Bind result-set to variables.
// Fetch record.
// Check # of Records Returned.
// Maximum Found.
// Increment CommentNo.
$commentNo = $lastCommentNo + 1;
// Maximum Not Found.
// Thrown an Error.
If no one has commented yet on the Article, then my ELSE branch would fire, and that is okay, so in that scenario I should "Do Nothing".
However, you could argue that the query could also fail, and then in that case my ELSE branch would fire again, but this time it should throw an Error.
See my confusion?
What would you advise?