barkster Posted March 27, 2006 Share Posted March 27, 2006 I'm trying to figure out why this doesn't work. I load a recordset on the page then run a function to return the value of a field but do not get a value but if I echo the field in the body it works. Why doesn't this work for me? Thanks[code]<?php require_once('Connections/MyCon.php'); ?><?php$colname_Users = "1";if (isset($_SESSION['MM_UserID'])) { $colname_Users = (get_magic_quotes_gpc()) ? $_SESSION['MM_UserID'] : addslashes($_SESSION['MM_UserID']);}mysql_select_db($database_DB, $MyCon);$query_Users = sprintf("SELECT zUserID, zFirstName, zLastName, zEmail, zUsername, Balance, zAddress, zCity, zState, zZip FROM zUsers WHERE zUserID = %s", $colname_Users);$Users = mysql_query($query_Users, $MyCon) or die(mysql_error());$row_Users = mysql_fetch_assoc($Users);$totalRows_Users = mysql_num_rows($Users);?><?phpfunction testme() {return 'here now'.$row_Users['zFirstName'];}?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title></head><body><?php echo(testme());//does not work ?><br /><?php echo $row_Users['zFirstName']; //works ?></body></html><?phpmysql_free_result($Users);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/5950-return-field-value/ Share on other sites More sharing options...
kenrbnsn Posted March 27, 2006 Share Posted March 27, 2006 That's because the array $row_Users is not known to your function. Variables defined outside a function are not known inside a function unless they are passed in via the function arguments or are declared global inside the function.One solution:[code]<?phpfunction testme($ary, $indx) {return 'here now'.$ary[$indx];}//////echo testme($row_Users,'zFirstName');?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/5950-return-field-value/#findComment-21315 Share on other sites More sharing options...
barkster Posted March 27, 2006 Author Share Posted March 27, 2006 Thanks, I see now. I was confused because I echo'd the value below the call to the function and it worked but I see what your saying. I thought they were global since I called it at the top of the page and used it in the body but I see what you saying about the function, not sending any information about the recordset to it. I couldn't get what you suggested to work for me but your comments got my function fixed up the way it needed to be. Can anyone tell me how to make this recordset public, just to know how to do it. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/5950-return-field-value/#findComment-21327 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.