avo Posted September 2, 2006 Share Posted September 2, 2006 HI allim just testing writing some functions but why will this not return the second or third $sql if the varible isset[code]function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit){ if (isset($contents) && isset($table) && isset($postname1)){ return $sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1.""; } if (isset($name2) && isset($postname2) ){ return $sql.=" AND ".$name2." = ".$postname2.""; } if (isset($limit)){ return $sql.="LIMIT ".$limit.""; }}[/code][quote]$test=Select('*',test_table,username,testname,'username2','testname2','2');echo($test);[/quote]?> Link to comment https://forums.phpfreaks.com/topic/19465-writing-functions/ Share on other sites More sharing options...
shocker-z Posted September 2, 2006 Share Posted September 2, 2006 try this..[code]function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit) { if (($contents !== '') && ($table !== '') && ($postname1 !== '')) { return $sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1.""; } if (($name2 !== '') && ($postname2 !== '') ) { return $sql.=" AND ".$name2." = ".$postname2.""; } if ($limit !== '')) { return $sql.="LIMIT ".$limit.""; }}[/code]i have changed to !== '' as this checks if there is a value but i think isset() will always return true because it is set at the top wether there is a value or not, i've also corected the if statement enclosures..RegardsLiam Link to comment https://forums.phpfreaks.com/topic/19465-writing-functions/#findComment-84530 Share on other sites More sharing options...
avo Posted September 2, 2006 Author Share Posted September 2, 2006 Hi thanks i used !empty()i've realized what i should have done .[code]function Select($contents,$table,$name1,$postname1,$name2,$postname2,$limit){ if (!empty($contents) && !empty($table) && !empty($postname1)){ $sql = "SELECT ".$contents." FROM ".$table." WHERE ".$name1." = ".$postname1.""; } if (!empty($name2) && !empty($postname2) ){ $sql.=" AND ".$name2." = ".$postname2.""; } if (!empty($limit)){ $sql.=" LIMIT ".$limit.""; } return $sql;}$test=Select('*','',test_table,testname,'','','1');echo($test);[/code] Link to comment https://forums.phpfreaks.com/topic/19465-writing-functions/#findComment-84531 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.