doubledee Posted June 18, 2012 Share Posted June 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264366-calculate-max-query-not-failing/ Share on other sites More sharing options...
doubledee Posted June 18, 2012 Author Share Posted June 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/264366-calculate-max-query-not-failing/#findComment-1354785 Share on other sites More sharing options...
trq Posted June 18, 2012 Share Posted June 18, 2012 SELECT MAX(blah) FROM tbl WHERE flb = 'whatever' Will always return 1 record. Quote Link to comment https://forums.phpfreaks.com/topic/264366-calculate-max-query-not-failing/#findComment-1354805 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.