Whats wrong with my code? (PHP and Mysql)

Associate
Joined
29 Dec 2004
Posts
2,252
Im trying to do the following:

function getPunboUserID()
{
global $database;

$query = "SELECT id FROM site_forums_users "
."WHERE username='.$my->username.'";
$database->setQuery( $query );
$Itemid = $database->loadResult();
return $Itemid;
}

But when i do this:

echo getPunboUserID();

It comes out as blank...however. If i change:

."WHERE username='.$my->username.'";

To:

."WHERE username='testuser'";

It outputs the id for testuser correctly.

If i just output $my->username (echo $my->username) it displays the currently logged in user as it should, but i dont know why its displaying nothing when i put it all together...

Have i got something wrong in this bit:::

."WHERE username='.$my->username.'";

??? Thanks for your help guys...
 
Code:
."WHERE username='.$my->username.'";
Should be either
Code:
."WHERE username='$my->username'"; (no dots)
or
Code:
."WHERE username='".$my->username."'";

By the looks of it you had full stops in your query so it didn't work.
If I have trouble with queries I usually just echo the query and then you can see what the problem is :)
 
Thanks guys, but for i tried both of them suggestions and its still blank...

When i echoed the query i get the following:

SELECT id FROM site_forums_users WHERE username='richieboy'

So thats working perfectly as it is passing the correct username into the query...but its just not outputting the users id number for some reason?

Is there a better way to do this rather than the function above? I basically copied it from a different function thats already in the page and adapted it to output what i wanted, its just not outputting it!

Appreciate your help :)
 
I suspect (though my PHP is a bit rusty, so I may be wide of the mark) it's that you're trying to access an object called $my that is out of scope. Try adding in:

global $my;

at the top of your function and see if that helps (assumes it has global scope and you didn't mean to pass it as a parameter to the function or something).
 
xyphic said:
I suspect (though my PHP is a bit rusty, so I may be wide of the mark) it's that you're trying to access an object called $my that is out of scope. Try adding in:

global $my;

at the top of your function and see if that helps (assumes it has global scope and you didn't mean to pass it as a parameter to the function or something).


It absolutely helped. Problem solved - i love you :)
 
Sorry rob i love you too pal, i realised that you had already given me the answer when i was just knocking off to sleep - so shall thank you now, i really appreciate everyones help. Seriously, i can make little changes and mess with code a bit - but when i actually need to do create new functions etc im an utter moron, and couldnt do it without you chaps! :)
 
Back
Top Bottom