Jump to content

Return Field Value?


barkster

Recommended Posts

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);
?>
<?php
function 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>
<?php
mysql_free_result($Users);
?>
[/code]
Link to comment
Share on other sites

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]<?php
function  testme($ary, $indx) {
return 'here now'.$ary[$indx];
}
//
//
//
echo testme($row_Users,'zFirstName');
?>[/code]

Ken
Link to comment
Share on other sites

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
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.