think tank forum

technology » I need some PHP help creating a function.

 
17 years ago
link
Mathew
Zombie Flesh Eaters
I have no idea why this doesn't work, but the "Return" doesn't seem to be working. Here is the code:

function profilesql($field,$userid) {
$sql = "select profile.$field from profile where profile.UserId = $userid";
$result = mysql_query($sql);
if ($result){
$row = mysql_fetch_assoc($result);
$rowout = $row["$field"];
//**an echo of rowout works here.**
}else{
$rowout = "";
}
return $rowout;
};

I don't want to just use an echo, I want to have the data returned. Thanks in advance.
 
17 years ago
link
Mathew
Zombie Flesh Eaters
I fixed it, I added the "print" function to the rowout, like:

function profilesql($field,$userid) {
$sql = "select profile.$field from profile where profile.UserId = $userid";
$result = mysql_query($sql);
if ($result){
$row = mysql_fetch_assoc($result);
$rowout = print($row["$field"]);
}else{
$rowout = "";
}
return $rowout;
};
nny's avatar
17 years ago
link
nny
M̮͈̣̙̰̝̃̿̎̍ͬa͉̭̥͓ț̘ͯ̈́t̬̻͖̰̞͎ͤ̇ ̈̚J̹͎̿̾ȏ̞̫͈y̭̺ͭc̦̹̟̦̭̫͊̿ͩeͥ̌̾̓ͨ
heh add code blocks to think tank forums... syntax highlighting + autoformat ftw
 
17 years ago
link
Mathew
Zombie Flesh Eaters
Damnit, it just prints. How the hell do I RETURN values? The first posting of code didn't work. Any ideas?
 
17 years ago
link
Mathew
Zombie Flesh Eaters
<code>
function profilesql($field,$userid) {
$sql = "select profile.$field from profile where profile.UserId = $userid";
$result = mysql_query($sql);
if ($result){
$row = mysql_fetch_assoc($result);
$rowout = $row["$field"];
}else{
$rowout = "";
}
return $rowout;
};
</code>
asemisldkfj's avatar
17 years ago
link
asemisldkfj
the law is no protection
syntax highlighting would be badass.

for now the pre tags are nice.


try putting the return bit inside each part of the if statement?

nah that seems wrong. umm, how are you testing if it returns anything?
asemisldkfj's avatar
17 years ago
link
asemisldkfj
the law is no protection
the first example here

http://us.php.net/return

has return inside the if statement as an example, so I guess you could do that. just seems redundant since you have an else statement.
sriehl's avatar
17 years ago
link
sriehl
surreal
you return one value which makes this easy:

when you call the function, do it like this:


$myreturnedvar = profilesql($sentvar);
lucas's avatar
17 years ago
link
lucas
i ❤ demo
yeah, or echo(profilesql($arg));
 
17 years ago
link
Mathew
Zombie Flesh Eaters
It's wierd, it seems that everytime I call the function (even when passing it into a variable) it echos....I don't know, I'm going to test a couple of things and let you guys know what I come up with.