A JM Posted June 17, 2009 Share Posted June 17, 2009 How should I handle zero records returned from my query? mysql_select_db($database_maxdbconn, $maxdbconn); $query_rstassign = "SELECT ID FROM users WHERE usergroup = 'adjuster' AND username = '" . $_SESSION['MM_Username'] . "'"; $rstassign = mysql_query($query_rstassign, $maxdbconn) or die(mysql_error()); $row_rstassign = mysql_fetch_assoc($rstassign); $activeADJ = $row_rstassign['ID']; The above query retrieves the ID of the user logged in. In some instances the ID retrieved when used in this later query will retrieve zero records, that's OK. $query_rstassign = "SELECT clnt_city, clnt_state, clnt_zipa FROM claimcue WHERE chk_new = 1 AND adj_id = " . $activeADJ . " ORDER BY clnt_zipa ASC"; How do I deal with my Dynamic Table that I'm creating when zero records are retrieved? This is the error that I'm seeing. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND ORDER BY clnt_zipa ASC LIMIT 0, 10' at line 1 Thanks. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/162660-solved-how-to-handle-0-records-returned/ Share on other sites More sharing options...
Mark Baker Posted June 17, 2009 Share Posted June 17, 2009 It's just ORDER BY, not AND ORDER BY Quote Link to comment https://forums.phpfreaks.com/topic/162660-solved-how-to-handle-0-records-returned/#findComment-858461 Share on other sites More sharing options...
A JM Posted June 18, 2009 Author Share Posted June 18, 2009 yes, thanks I cut down the SQL statement since it was redundant. I still have the zero records returned issue though. A JM, Quote Link to comment https://forums.phpfreaks.com/topic/162660-solved-how-to-handle-0-records-returned/#findComment-858537 Share on other sites More sharing options...
Maq Posted June 18, 2009 Share Posted June 18, 2009 yes, thanks I cut down the SQL statement since it was redundant. I still have the zero records returned issue though. A JM, You can use mysql_num_rows to check how many rows have been returned. If 0, then display a certain message, if not, then proceed with rest of code. mysql_select_db($database_maxdbconn, $maxdbconn); $query_rstassign = "SELECT ID FROM users WHERE usergroup = 'adjuster' AND username = '" . $_SESSION['MM_Username'] . "'"; $rstassign = mysql_query($query_rstassign, $maxdbconn) or die(mysql_error()); if(mysql_num_rows($tstassign) == 0) { echo "Sorry, ID not found"; // Maybe assign a default ID } else { // Proceed with rest of code $row_rstassign = mysql_fetch_assoc($rstassign); $activeADJ = $row_rstassign['ID']; } Quote Link to comment https://forums.phpfreaks.com/topic/162660-solved-how-to-handle-0-records-returned/#findComment-858582 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.