By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

Passing variables out of a function

P: n/a
For whatever reason I can't get my return value to pass outside of the
function I've created. I've tested all the responses within the
function so I know the data is there and the sql query is valid. Am I
missing something to get it passed outside the function?

******************************

function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username,$userx)) {
$freturn = 7;
return $freturn;
}
}
}

******************************

May 18 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On May 18, 9:18 am, Akhenaten <jonko...@gmail.comwrote:
For whatever reason I can't get my return value to pass outside of the
function I've created. I've tested all the responses within the
function so I know the data is there and the sql query is valid. Am I
missing something to get it passed outside the function?

******************************

function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username,$userx)) {
$freturn = 7;
return $freturn;

}
}
}

******************************
Disregard -- it's always the simple mistakes that make you pound your
head. I failed to make $freturn global.

May 18 '07 #2

P: n/a
On May 18, 10:47 am, Akhenaten <jonko...@gmail.comwrote:
On May 18, 9:18 am, Akhenaten <jonko...@gmail.comwrote:
For whatever reason I can't get my return value to pass outside of the
function I've created. I've tested all the responses within the
function so I know the data is there and the sql query is valid. Am I
missing something to get it passed outside the function?
******************************
function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username,$userx)) {
$freturn = 7;
return $freturn;
}
}
}
******************************

Disregard -- it's always the simple mistakes that make you pound your
head. I failed to make $freturn global.
The whole point of returning it from the function is so you don't have
to make it a global.

May 18 '07 #3

P: n/a

"Akhenaten" <jo******@gmail.comwrote in message
news:11**********************@u30g2000hsc.googlegr oups.com...
On May 18, 9:18 am, Akhenaten <jonko...@gmail.comwrote:
>For whatever reason I can't get my return value to pass outside of the
function I've created. I've tested all the responses within the
function so I know the data is there and the sql query is valid. Am I
missing something to get it passed outside the function?

******************************

function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users`
WHERE `qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username,$userx)) {
$freturn = 7;
return $freturn;

}
}
}

******************************

Disregard -- it's always the simple mistakes that make you pound your
head. I failed to make $freturn global.
are you sure your if statement is being called? BTW, global has nothing to
do with this.

you do not return a result in all return paths so how can you be sure?
function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users` WHERE
`qid` = $qid AND `a_uid` = `userid` LIMIT 0, 30",$db);
while ($userx=mysql_fetch_array($postchk2))
{
if (in_array($username,$userx))
{
return TRUE;
}
}

return FALSE;
}
I didn't check the logic but chances are you also need to check $postchk2.
if its returning false then your loop won't be called... also not sure what

while ($userx=mysql_fetch_array($postchk2))

is doing.

If your trying to check and see if the user is in the table then you should
probably just check the result's number of rows.

i.e.

function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users` WHERE
`qid` = $qid AND `a_uid` = `userid` AND `username` = '$username' LIMIT 0,
30",$db);
if (mysql_num_rows($postchk2) 0)
{
return TRUE;
}

return FALSE;
}
This function should return true if the user is found and false if not. Note
that I added

AND `username` = '$username'

This is what your tryign to get returned but you can have sql do the work
for you. It will only return the results where that is true but ofcourse
thats all you care about anyways. Technically if its something like a user
login you might want to test for more than one but in a well setup system
there shouldn't be any more than 1. You'll also still needt o make sure that
$postchk2 has a valid result in it.

Jon

May 18 '07 #4

P: n/a
function respond_check($qid,$username)
{
include "db.inc.php";
$postchk2 = mysql_query("SELECT username FROM `answers` , `users` WHERE
`qid` = $qid AND `a_uid` = `userid` AND `username` = '$username' LIMIT 0,
30",$db);
if (mysql_num_rows($postchk2) 0)
{
return TRUE;
}

return FALSE;

}

This function should return true if the user is found and false if not. Note
that I added

AND `username` = '$username'

This is what your tryign to get returned but you can have sql do the work
for you. It will only return the results where that is true but ofcourse
thats all you care about anyways. Technically if its something like a user
login you might want to test for more than one but in a well setup system
there shouldn't be any more than 1. You'll also still needt o make sure that
$postchk2 has a valid result in it.

Jon

I copied your function verbatim Jon (thx for the sql advice by the
way). Unfortunately, I am unable to pull the data (TRUE or FALSE) from
the function - almost like it's not even executing. Any thoughts how I
can test why the function is not being called?

May 18 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.