Jump to content

Correct way to check for boolean values returned from mysql_result


RedRocky

Recommended Posts

What is the correct way to check for true or false when returning a value from a boolean field using mysql_result?

 

I have been using the following to check for false:

if (mysql_result($MyDataset,0,'APPROVED') == 0)

However, mysql_result returns FALSE if the function fails, which would also be equal to 0 in the above code.

 

Is the correct way to check for false in the boolean field as follows:

if (mysql_result($MyDataset,0,'APPROVED') === '0')

and the correct way to check for true as follows:

if (mysql_result($MyDataset,0,'APPROVED') === '1')

 

Thanks in advance for your help.

Link to comment
Share on other sites

If your query is failing with an error, there's no point in trying to use the data. You would need to find and fix whatever problem is causing the query to fail and enclose the code that uses the data from the query inside of logic so that you only access the data when the query executes without any error.

Link to comment
Share on other sites

What is the data type of the field, and what values are stored in it?

The data type is boolean, which in MySQL administrator is tinyint(1).

 

The values stored in it are 1 for true and 0 for false.

 

PFMaBiSmAd, the query isn't failing, but if mysql_result can return false then don't I want to make sure I can tell the difference between false for the field value and false in case the query fails?

Link to comment
Share on other sites

What is the data type of the field, and what values are stored in it?

The data type is boolean, which in MySQL administrator is tinyint(1).

The values stored in it are 1 for true and 0 for false.

PFMaBiSmAd, the query isn't failing, but if mysql_result can return false then don't I want to make sure I can tell the difference between false for the field value and false in case the query fails?

 

This is all highly academic, and I wouldn't use this function in this way as it shows how needlessly complicated this method is.

 

$MyDataset = mysql_query....

if ($MyDataset && mysql_num_rows($MyDataset) > 0) {
  // There is no reason that you should have an error inside here, other than if your query is simply incorrect.
  // Just for academic purposes, here is how you could write some pessimistic code
  $value = mysql_result($MyDataset,0,'APPROVED');

  if ($value === false) {
    // do whatever you want to do when you have an error getting the result of an otherwise valid query
  } else {
    $approved = ($value = 1) ? true : false;
  }
}

Link to comment
Share on other sites

Yes, good catch.  I just type this stuff off the top of my head, but usually I don't make those types of blunders.  Kuddos to you for actually reading the code.  It seems all too often we get people asking questions here who really don't know php at even a beginner's level.

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.