SELECT MAX(comment_no) AS lastCommentNo
FROM comment
WHERE article_id = ?

I'm trying to build error-handling for it in PHP, and am not sure I understand the possible scenarios?! :-/

Sincerely,

Debbie

DaveMaxwell
—
2013-06-05T18:28:49Z —
#2

If nothing matches the criteria, you'll get a null result, so you need to be able to handle that accordingly.

DoubleDee
—
2013-06-05T18:45:14Z —
#3

DaveMaxwell said:

If nothing matches the criteria, you'll get a null result, so you need to be able to handle that accordingly.

But can a query like this in MySQL just "blow up", "crash", "burn", etc? :-/

(Maybe I don't really understand the inner workings of databases...)

I am trying to handle 3 - perceived - scenarios...

1.) The query find the largest "Comment No" for a given Article

2.) The query blows up

3.) The query finds no Comments for a given Article

My fear is that if I combine #2 and #3 together and just say "A NULL return is okay" that there might be something bad that could happen?!

Follow me??

Debbie

DaveMaxwell
—
2013-06-05T19:11:39Z —
#4

The only time queries truly "blow up" is if you're trying something with discordant datatypes, like performing a numeric comparison with a character value, or trying to imply conversions for some of the larger datatypes (CLOBS to strings for example).

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.

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.

DoubleDee
—
2013-06-05T19:22:42Z —
#5

DaveMaxwell said:

The only time queries truly "blow up" is if you're trying something with discordant datatypes, like performing a numeric comparison with a character value, or trying to imply conversions for some of the larger datatypes (CLOBS to strings for example).

Hmm, okay.

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?

Actually, your logic is flawed - the $lastCommentNo would be set - it would be null, but it would be set....

DoubleDee
—
2013-06-05T21:19:20Z —
#7

DaveMaxwell said:

Actually, your logic is flawed - the $lastCommentNo would be set - it would be null, but it would be set....

It's not flawed.

In PHP, isset() determines if a variable is set and is not NULL.

That is what my code does.

I have been asking about the ELSE branch of my code, i.e. should I just set $lastCommentNo = 1 and call things acceptable, or do I get neurotic, and first run a query that checks the COUNT() of Comments, and if it is non-zero, THEN run the code above.

Your earlier post suggests that I can assume the SELECT MAX() won't "blow up", and so if I get a "NULL", then I can do as I suggest above...

Sincerely,

Debbie

DaveMaxwell
—
2013-06-06T01:10:04Z —
#8

You're right. That's the downfall to writing in different languages.

Why throw an error, though? No comments seems like a valid situation....

DoubleDee
—
2013-06-06T03:22:02Z —
#9

DaveMaxwell said:

You're right. That's the downfall to writing in different languages.

Why throw an error, though? No comments seems like a valid situation....

Again, because I was thinking that my SELECT MAX() could fail, and also return a NULL under some other condition besides there just simply not being any Comments.