|
Posted by Jon Slaughter on 05/18/07 16:38
"Akhenaten" <jonkokko@gmail.com> wrote in message
news:1179499640.506421.251980@u30g2000hsc.googlegroups.com...
> On May 18, 9:18 am, Akhenaten <jonko...@gmail.com> wrote:
>> 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
[Back to original message]
|