Jump to content

Writing functions


avo

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]
?>
Link to comment
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
Link to comment
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]
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.