Jump to content

Archived

This topic is now archived and is closed to further replies.

avo

Writing functions

Recommended Posts

HI all

im 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]
?>

Share this post


Link to post
Share on other sites
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..

Regards
Liam

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.