Jump to content

Calculate Max # Query not Failing?!


doubledee

Recommended Posts

This is going to sound weird, but I am trying to break my latest query to make sure my error-handling code is working.

 

Turns out it is NOT working?!

 

Here is my code...

 

// **********************
// Find Last CommentNo.	*
//***********************

// 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', $articleID2);

// Execute query.
mysqli_stmt_execute($stmt2);

// Store results.
mysqli_stmt_store_result($stmt2);

// 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.
	mysqli_stmt_fetch($stmt2);

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

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

	// Set Error Source.
	$_SESSION['errorPage'] = $_SERVER['SCRIPT_NAME'];

	// Redirect to Display Outcome.
	header("Location: " . BASE_URL . "/account/results.php");

	// End script.
	exit();

}//End of FIND LAST COMMENT-NO

 

 

The way I usually test my error-handling is to change a variable name in the Bind statement like this...

 

// Bind variable to query.

mysqli_stmt_bind_param($stmt2, 'i', $articleID2);

 

Here I added a "2" onto $articleID which should make my query fail, but in this case it doesn't?!

 

I am thinking this has something to do with my use of MAX() ?!

 

Right now the query runs but returns "1" for the CommentNo when I was expecting "10" as the next Comment Number...

 

 

 

Debbie

 

 

Link to comment
Share on other sites

If I change my query to this...

 

// Build query.
$q2 = "SELECT 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.
mysqli_stmt_execute($stmt2);

// Store results.
mysqli_stmt_store_result($stmt2);

// 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.
	mysqli_stmt_fetch($stmt2);

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

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

 

 

...then the query "fails" because several values are returned and my Error Message fires.

 

 

So my question is, if I change $articleID to $articleID2 in my original query, then that means there would be no valid Article ID (i.e. $articleID2 would equal "0") and so my query would return NULL.

 

I guess that is where the problem is occurring.

 

My query is "returning 1 row" which is NULL, but that isn't what I want.

 

Follow me?

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.