View Full Version : Query not throwing proper error

06-18-2012, 10:48 AM
I have the following code that is not throwing an error like I am used to...

// Build query.
$q2 = "SELECT MAX(comment_no) AS lastCommentNo
FROM comment
WHERE article_id=?";

// Prepare statement.
$stmt2 = mysqli_prepare($dbc, $q2);

// Bind variable to query.
mysqli_stmt_bind_param($stmt2, 'i', $articleID);

// Execute query.

// Store results.

// Check # of Records Returned.
if (mysqli_stmt_num_rows($stmt2)==1){
// Maximum Found.

// Bind result-set to variables.
mysqli_stmt_bind_result($stmt2, $lastCommentNo);

// Fetch record.

// Increment CommentNo.
$commentNo = $lastCommentNo + 1;

// Maximum Not Found.
$_SESSION['resultsCode'] = 'COMMENT_MAXIMUM_NOT_FOUND_2049';

Normally to test the "Error Branch" of my code above, I would change $articleID to $articleID2, thus breaking my query and launching the ELSE branch of my code with the Error-Message.

The problem seems to be that $articleID2=0 and so my query returns a NULL for the MAX() which technically counts as a row being returned?!

Follow me?!

That isn't the behavior I am looking for.


06-18-2012, 01:32 PM
how about

$q2 = "SELECT MAX(comment_no) AS lastCommentNo
FROM comment
WHERE article_id='debbie'";