Solved

Hopw do i return a true or false result from a function

Posted on 2007-12-04
12
238 Views
Last Modified: 2010-04-21
I have created this function...
function ActivateAccount(){
      global $database;
      $pass = stripslashes($_GET['hash']);
      $user = base64_decode($_GET['stamp']);
      $q = "UPDATE ".TBL_USERS." SET status = '1' WHERE (password = $pass) AND (username = '$user')";
      $database->query($q);
}

Would like to know, how to pass some kind of string or something that i can use later on in the document...

like...

<? if($status == 0){
?>
account not activated
<? }else{ ?>
account activate!
<? } ?>

How do I do this?
0
Comment
Question by:Alan-Yeo
  • 4
  • 4
  • 4
12 Comments
 
LVL 27

Accepted Solution

by:
yodercm earned 400 total points
ID: 20402210
if ($status == 0)
  return TRUE;
else
  return FALSE:
0
 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 400 total points
ID: 20402214
Or you could return the string itself with

if ($status == 0)
  return "account not activated";
else
  return "account activated;

and then where you call the function, just use

$activatedresult = ActivateAccount;

and the variable $activatedresult will contain whichever of those strings applies.
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20402215
Sorry, missed a " mark :))

if ($status == 0)
  return "account not activated";
else
  return "account activated";
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 20

Expert Comment

by:steelseth12
ID: 20402221
function ActivateAccount(){
      global $database;
      $pass = stripslashes($_GET['hash']);
      $user = base64_decode($_GET['stamp']);
      $q = "UPDATE ".TBL_USERS." SET status = '1' WHERE (password = $pass) AND (username = '$user')";
      $database->query($q);
        
        if($database->affected_rows){
        
              return "account activate!";
        
        }else{
        
              return "account not activated";
        
        }
        
        
}
0
 

Author Comment

by:Alan-Yeo
ID: 20402674
ok ... still not working as i'd like.

<? include("include/session.php");
function ActivateAccount(){
      global $database;
      $pass = stripslashes($_GET['hash']);
      $user = base64_decode($_GET['stamp']);
      $q = "UPDATE ".TBL_USERS." SET status = '1' WHERE (password = $pass) AND (username = '$user')";
      $database->query($q);
    if($database->affected_rows){
            return true;
      }else{
            return false;
      }
}
?>

<?
$activatedresult = ActivateAccount();
if($activatedresult = true){
      echo "congratulations";
}else{
      echo "boo hoo ";
}

IT always returns congratulations... even when the sql database is not updated.
0
 
LVL 20

Assisted Solution

by:steelseth12
steelseth12 earned 100 total points
ID: 20402696
if($activatedresult =  true){

should be

if($activatedresult == true){

= is an assignment operator .. you have to use == for comparisons
0
 

Author Comment

by:Alan-Yeo
ID: 20402728
ok, stil not working... "boo hoo" is displayed even though the sql database has been updated.
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20402756
put quotes around $pass in your query

 $q = "UPDATE ".TBL_USERS." SET status = '1' WHERE (password = '$pass') AND (username = '$user')";
0
 

Author Comment

by:Alan-Yeo
ID: 20402796
the query doesn't work with quotes around $pass
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20402827
you get an error ?

try the following lines

$q = "UPDATE ".TBL_USERS." SET status = '1' WHERE (`password` = '$pass') AND (username = '$user')";

$database->query($q) or die($database->error);
0
 

Author Closing Comment

by:Alan-Yeo
ID: 31412556
used parts of all your solutions. Thanks for the help
0
 
LVL 27

Expert Comment

by:yodercm
ID: 20403034
$q = "UPDATE TBL_USERS SET status = '1' WHERE (password = '$pass') AND (username = '$user')";
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question