468,256 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

Passing variables out of a function

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
4 1823
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
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

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

Similar topics

4 posts views Thread by Amr Mostafa | last post: by
1 post views Thread by Consuelo Guenther | last post: by
3 posts views Thread by domeceo | last post: by
12 posts views Thread by Dennis D. | last post: by
12 posts views Thread by Andrew Bullock | last post: by
7 posts views Thread by amygdala | last post: by
1 post views Thread by August Karlstrom | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.