Jump to content

if(boolean value)


PC Nerd

Recommended Posts

hey guys

 

i have code similar to this:

 

if($Research['1_00'] != True) {

###DISPLAY ERROR MESSAGE

}

 

else {

### Do stuf

}

 

 

 

now my problem is that i have checked my database and the $Research['1_00'] value IS TRUE ( 1 in phpmyadmin ) but this code doesnt work, it raises the error

now is this a problem with my testing of the boolean value, or is it with the database

 

 

THANKX

Link to comment
Share on other sites

if you use 1 and 0 then thats not really checking if it is true or false, i dont think. you'd have to check whether it was 1 or 0.

and just so you no, if you place a ! in front of a variable you are checking if it is false. thats assuming the variable is a boolean (true or false). if you want to check if it is true just use

if($variable){

and it would check if it is set to true.

Link to comment
Share on other sites

if you're saying how to avoid the auto typcasting?

 

use !== and ===

 

$test = "true";
if($test === true){
   echo 'Test';
}

 

this wouldn't output anything because $test is a string, the same rules apply to !==

 

$test = false;
if($test !== "false"){
   echo 'Test';
}

 

this will output "test" because $test is bool and you're seeing if it's "false" (a string)

 

sorry if I missed your question, I'm tired

Link to comment
Share on other sites

ok

 

this is probably a question for poeple who use phpmyadmin.

 

when you have a boolean field ( implemented as intyint(11) i think) )and the datase says "1" (without the quotes) does it mean that the value is TRUE.  or is the 1, showing you that the  field is a text or other, and the 1 is a string or integer???

 

 

i tried the if(!$Research['1_00']){}

 

and it still raisees the error.

 

 

,my full code around this probmel part is....:

 

 

<?

### connect to the database and retreive research information
require('inc_files/Database_link.inc');

$Research_SQL = "SELECT * FROM General_Stats WHERE User_ID = ".$_COOKIE['User_ID'];#User_ID, 1_00, 1_01, 1_02, 1_03, 1_04, 1_05, 1_06, 1_07, 1_08, 1_09, 1_10, 1_11, 1_12, 1_12, 1_13, 1_14, 1_15, 1_16, 1_17, 1_18, 1_19, 1_20, 1_21, 1_22, 1_23 FROM Ice_Ages WHERE User_Name = ".$_COOKIE['User_ID'];

$Research_Query = mysqli_query($DB_Server, $Research_SQL)

or die("ERROR 5 There was an error in retreiving your research status from the database.  Please try again later, and if the problem persists please write down the error number, and email us with this Number");

$Research = mysqli_fetch_array($Research_Query)

or die("ERROR 6 There was an error while using your research status.  Please try again later.");

### Test Research Data to determine the next option of reasearch



if (!$Research['1_00']) {
die ("ERROR 7 ");
}


if ($Research['1_01'] == "True") {
if($Research['1_02'] == "True") {
	if($Research['1_10'] != "True") {..........

 

 

ok, ignore the if's after the first tesgint eh ['1_00']  i need to shcnge them to work.  but its the first if thats cauaseing me trouble

 

 

thankx

Link to comment
Share on other sites

alright. this is what i do. if the value is false i set it to 0 in my database. so the field type is tinyint. then when checking the value i'd go:

 

if($Research['1_00'] == 0){

 

that would check if it is false or no, which is another thing i use 0 and 1 for. 0 = no, 1 = yes

Link to comment
Share on other sites

FALSE ALRAM

 

soz guys

 

 

its giving me output, although its wrong output.

 

whats happening in some cases is its the inverse operation if !=1 give the output as !=0

 

but other times, it just defults back to the first if()   wthich isnt very good.

 

 

my database if a bool, but when i manually enter data into the fieldsd as the record im veiwing....... i type true into the boolean fieild and it ends up as 0...... and the same as if i went 1 instead of true.

although is sometimes worked if the field was text type with true as the value....

 

 

its really patchy, not the same every time.... can you help me

 

 

 

Now if i change from == 1 TO !=1, it gives the correct output....

 

 

if ($Research['1_00'] == 0) {
}
else {
die ("ERROR 7 ");
}


if ($Research['1_01'] == 1) {
if($Research['1_02'] == 1) {
	if($Research['1_10'] != 1) {
		$Res_avail['1_10'] = "True";
	}
	if($Research['1_03'] == 1) {
		if($Research['1_05'] == "True") {
			if($Research['1_06'] == "True") {
				if($Research_['1_07'] != "True") {
					$Res_Avail = "True";
				}
				if($Research['1_08'] != "True") {
					$Res_Avail = "True";
				}
			}
			if($Research['1_09'] != "True")	{
				$Res_Avail['1_09'] = "True";
			}

		}
		else {
		$Res_Avail['1_05'] = "True";
		}

		$Res_Avail['1_04'] = "True";
	}
	else {
	$Res_Avail['1_03'] = "True";
	}
}
else {
$Res_Avail['1_02'] = "True";
}
}
else {
$Res_Avail['1_01'] = "True";
}

 

this is the first section of code..... the database ass it stands is a boolean type, and with the values of 1 for 1 - 5, but 6 - onwards ( $Research array) are all text and empty ( NULL)  so they should equate to being false ( empty ( if($Research['1_06'] != 1) {will turn true, becauase its NULL in the Database}))

 

 

thankz

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.