RedRocky Posted February 18, 2012 Share Posted February 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/ Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2012 Share Posted February 18, 2012 What is the data type of the field, and what values are stored in it? Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318611 Share on other sites More sharing options...
PFMaBiSmAd Posted February 18, 2012 Share Posted February 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318614 Share on other sites More sharing options...
RedRocky Posted February 18, 2012 Author Share Posted February 18, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318646 Share on other sites More sharing options...
gizmola Posted February 18, 2012 Share Posted February 18, 2012 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318652 Share on other sites More sharing options...
RedRocky Posted February 18, 2012 Author Share Posted February 18, 2012 $approved = ($value = 1) ? true : false; Should this line be: $approved = ($value == 1) ? true : false; Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318658 Share on other sites More sharing options...
gizmola Posted February 18, 2012 Share Posted February 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/257246-correct-way-to-check-for-boolean-values-returned-from-mysql_result/#findComment-1318661 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.