Jump to content

Archived

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

garry27

return variable in functions

Recommended Posts

hi all,

how do you return a value from inside a function which you can use when you call it? i.e. in the following code, i'm trying to return the value of $account_type to the login function:

                        [code]if(login($_POST)== 'P')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'pa_home.php';
}

if(login($_POST)== 'B')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'ba_home.php';
}
                          if(!login($_POST))
echo 'membership not recognised';[/code]

TIA

Garry
 

     

Share this post


Link to post
Share on other sites
There's no function there so nothing to return from.

[code]
<?php
  // Returning a single value
  function Func(){
    $ReturnValue = 'Hello, World!';
    return $ReturnValue;
  }

  // Returning multiple values
  function FuncMany(){
    $Ret = Array();
    $Ret['Val1'] = $Val1;
    $Ret['Val2'] = $Val2;
    return $Ret;
  }
?>[/code]

Share this post


Link to post
Share on other sites
i've already defined the function login:
           
[code]...
        $sql = "select Member.accType from Member
                  Where Member.userEmail='$email";
   
        $account_type = $conn->query($sql);
        return $account_type;
        } [/code]

now i'm trying to call the value of $account_type using post:

[code]                if(login($_POST)== 'P')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'pa_home.php';
}

if(login($_POST)== 'B')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'ba_home.php';
}
                          if(!login($_POST))
echo 'membership not recognised';[/code]

Share this post


Link to post
Share on other sites
Post your [i]login[/i] function.

Share this post


Link to post
Share on other sites
i've been stuck on this for well over an hour now. any help would me much appreciated.



[code]
function login($email, $pwd)
{
        $conn = db_connect($account_type);
        $sql = "select * from All_Users where userEmail='$email' and userPassword='$pwd'";   
        $result=$conn->query($sql);
 
        if ($result->num_rows >0 )
{
  $sql = "select Member.accType from Member
                                    Where Member.userEmail='$email'";
$account_type = $conn->query($sql);

}
        return $account_type ;[/code]
       





[code]<?php

//Initiates php script containing functions all common functions
session_start();
require_once('common.php');
$dbconnect = db_connect();

//outputs header info to brower with the following title
echo xhtmlheader('Personal Account Registration Form');
 
//outputs the main navigation bar to the browser
echo topnav_pa();




if (isset($_POST['email']) && isset ($_POST['pwd']))
{

        //if the user has just tried to log in
        $email = $_POST['email'];
        $pwd = $_POST['pwd'];
 
       
        if(login($_POST)== 'B')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'pa_home.php';
}

if(login($_POST)== 'P')
{
$_SESSION['valid_user'] = $email;
$member_homepage = 'ba_home.php';
}
        if(!login($_POST))
echo 'membership not recognised';
}
?>

<!-- main body of page -->
<div id="main">
  <h1>My Pub Crawl Planner</h1>
 



<?php
  if (isset($_SESSION['valid_user']))
  {
    echo '<h1> You are logged in as: '.$_SESSION['valid_user'].' </h1><br />';
echo "<a href=$member_homepage>go to home page</a>";
echo "<a class='logout' href='logout.php'>Log out</a>";
  }
  else
  {
    if (isset($email))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in.<br />';
    }
    else
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
  }
?>
       
<p>Member Login </p>


<?php

echo show_loginfrm();

?>[/code]<!-- main body of page -->
<div id="main">
  <h1>My Pub Crawl Planner</h1>
 




[code]
<?php
  if (isset($_SESSION['valid_user']))
  {
    echo '<h1> You are logged in as: '.$_SESSION['valid_user'].' </h1><br />';
echo "<a href=$member_homepage>go to home page</a>";
echo "<a class='logout' href='logout.php'>Log out</a>";
  }
  else
  {
    if (isset($email))
    {
      // if they've tried and failed to log in
      echo 'Could not log you in.<br />';
    }
    else
    {
      // they have not tried to log in yet or have logged out
      echo 'You are not logged in.<br />';
    }
  }
?>
       
<p>Member Login </p>


<?php

echo show_loginfrm();

?>
[/code]

Share this post


Link to post
Share on other sites
i can log on now afer i deleted the stupid variable after db connection on login() but i'm still getting:

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38

:

[code]
function login($email, $pwd)              //line 38
{
        $conn = db_connect();
        $sql = "select * from All_Users where userEmail='$email' and userPassword='$pwd'";   
        $result=$conn->query($sql);
 
        if ($result->num_rows >0 )
{
  $sql = "select Member.accType from Member
                  Where Member.userEmail='$email'";
  $account_type = $conn->query($sql);

    }
        return true;
}[/code]

???

Share this post


Link to post
Share on other sites
it's because you've constructed your login function such that it requires you pass it the email and password that the user is attempting to login with.  NOT the array they're found in.  try this:

[code]login($_POST['email'], $_POST['pwd'])[/code]

you can change your function back to returning the account_type variable, that wasn't the issue.  read a little bit on functions to learn how parameters and arguments work with custom functions.  a bit of reading on scope would help as well.

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.