garry27 Posted October 16, 2006 Share Posted October 16, 2006 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]TIAGarry Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/ Share on other sites More sharing options...
roopurt18 Posted October 16, 2006 Share Posted October 16, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109772 Share on other sites More sharing options...
garry27 Posted October 16, 2006 Author Share Posted October 16, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109780 Share on other sites More sharing options...
trq Posted October 16, 2006 Share Posted October 16, 2006 Post your [i]login[/i] function. Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109798 Share on other sites More sharing options...
garry27 Posted October 17, 2006 Author Share Posted October 17, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109808 Share on other sites More sharing options...
garry27 Posted October 17, 2006 Author Share Posted October 17, 2006 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 38Warning: Missing argument 2 for login() in /home/unn_p921847/public_html/common.php on line 38Warning: 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] ??? Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109818 Share on other sites More sharing options...
garry27 Posted October 17, 2006 Author Share Posted October 17, 2006 why isn't it working dammit? :( Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109830 Share on other sites More sharing options...
akitchin Posted October 17, 2006 Share Posted October 17, 2006 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. Link to comment https://forums.phpfreaks.com/topic/24152-return-variable-in-functions/#findComment-109837 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.