MySQL NULL fix PHP case switch fix 0

Post Reply
darknkreepy3#
Site Admin
Posts: 247
Joined: Tue Oct 27, 2009 9:33 pm

MySQL NULL fix PHP case switch fix 0

Post by darknkreepy3# »

So, when you make a mysql call with mysql, mysqli etc, you might have built a table with a field containing no value, or a NULL value.
When you import that with mysqli_fetch_assoc you will find that a NULL is identified loosely in the SWITCH command and represented as false and or o.

Check your value in the row['*'] replacing * with your field and find out if it is_null with PHP this way

Code: Select all

			//
			if(is_null($_a))
				{
				$_a=2;
				}
So an inline example would be the following where I am color customizing a field defined by if a person is active or not in a dancer program I am writing.

Code: Select all

	//
	$table="dancers";	
	$connectG=mysqli_connect($server,$user,$pass,$db);
	$dancerQueryStr="SELECT * FROM $table";
	$queryD=mysqli_query($connectG,$dancerQueryStr);
	//echo mysqli_error($connect)."<br />";
	$lenG=mysqli_num_rows($queryD);	
	//
	for($a=0;$a<$lenG;$a++)
		{
		$row=mysqli_fetch_assoc($queryD);
		$danceArr[]=$row;
		}
	mysqli_close($connectG);

	//
	function dancers_wide($sArr)
		{
		$lenD=count($sArr);
		$dStr="";
		//
		for($b=0;$b<$lenD;$b++)
			{
			$classStr="";
			$_a=$sArr[$b]['_active'];
			/*
			loose connection in php's switch
			0 is not active
			1 is active
			NULL or IS_NULL is 0 as false etc, so check and replace with 2 if null
			otherwise, NULL will also be 0 and absorbed with case(0)
			*/
			//
			if(is_null($_a))
				{
				$_a=2;
				}
			//echo "[".gettype($_a)."][$_a]";
			//choose div color and style
			//
			switch($_a)
				{
				case(0):
					$classStr="dancerWideCol_inactive";
					$_active=0;
					break;
				case(1):
					$classStr="dancerWideCol_active";
					$_active=1;
					break;
				case(2):
					$classStr="dancerWideCol_undefined";
					$_active=-1;
					break;
				}
			
			$d_title="<div class='d_title'>".$sArr[$b]['_name']."</div>";
			$b_active="<div class='b_active' _active='".$_active."' id='".$sArr[$b]['id']."'>A</div>";
			$b_couch="<div class='b_couch' onclick='addDancerToCouchArea($b);'>C</div>";
			$b_dance="<div class='b_dance' onclick='addToStage($b);'>D</div>";
			$cB="<div class='clearBoth'></div>";			
			

			$dStr.="
				<div class='$classStr'>".$d_title.$b_active.$b_couch.$b_dance.$cB."</div>
			";
			}
		return $dStr;
		}
Post Reply