Jump to content

Recommended Posts

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,

 

Link to comment
https://forums.phpfreaks.com/topic/162660-solved-how-to-handle-0-records-returned/
Share on other sites

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'];
}

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.