Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.
Not a member yet? Click here to register.
Forgot Password?

function + $userdata is failing

Asked Modified Viewed 4,269 times
G
googlebot
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
  • Started this discussions
asked
Senior Member

I don't know if this is a bug or not. Although what I'm trying to do makes perfect sense, so I don't understand why it isn't working, hence I'm posting here.

I am writing my own functions now. And it has been going great until I tried to put an SQL command in a function, that has a conditional (WHERE user_id = ".$userdata['user_id']."wink

And I was experimenting after getting nothing for the user_id. I tried just echoing the user_id with

echo $userdata['user_id'];

and it didn't work. It didn't display anything, not even a zero.

And then I tried echoing with that same exact command, on the line before the function started, so that it wasn't a part of the function. And it worked! And now I'm really, really confused.. Can you simply not use the $userdata variable in functions, or any db queries in functions? And if you can't, how might I get around this issue?

Thanks!
googlebot

Edit reason: disable smileys.
Edited by googlebot on 03-01-2010 04:11,
0 replies

11 posts

P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

global $userdata;

-- insert in [header of] your function.
0 replies
B
Basti
B
Basti 10
[PHP-Fusion Crew Member & Admin from June 2008 - December 2010]

http://basti2web.de - Support Site for my infusions
  • Veteran Member, joined since
  • Contributed 1,099 posts on the community forums.
  • Started 32 threads in the forums
answered
Veteran Member

here an example:

$var1 = "as";
$var2 = "as";
$var3 = "as";

testme($var3);

function testme($var4){
global $var1;

echo $var1; // works
echo $var2; // works not
echo $var3; // works not
echo $var4; // works
}
Edited by Basti on 03-01-2010 14:48,
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

I sayed global ...
0 replies
G
googlebot
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
  • Started this discussions
answered
Senior Member

Thanks so much guys!

Thanks PolarFox for the solution, and thanks slaughter for the example; now I understand functions more. :) I just started writing custom functions yesterday, so it didn't make sense to me. But now it does. :)
0 replies
A
afif
A
afif 10
  • Member, joined since
  • Contributed 183 posts on the community forums.
  • Started 12 threads in the forums
answered
Member

what does global really do?
0 replies
B
Basti
B
Basti 10
[PHP-Fusion Crew Member & Admin from June 2008 - December 2010]

http://basti2web.de - Support Site for my infusions
  • Veteran Member, joined since
  • Contributed 1,099 posts on the community forums.
  • Started 32 threads in the forums
answered
Veteran Member

Quote

afif wrote:
what does global really do?


http://php.net/manual/en/language.var....scope.php
0 replies
G
googlebot
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
  • Started this discussions
answered
Senior Member

Thanks for the help guys, but I've run into...sort of the reverse of this problem now:

In short I have these functions:



function test($var) {
echo $var;
}

function testing() {
$var2 = $var+1;
echo $var2;
}

And I want to make the $var variable a global, so that other functions can use it even though it is only declared in the test function. I have a similar layout to this with a couple of functions on about 30 pages, so I am looking for a simple way to edit the two functions to accomplish this, without having to edit every single file.

Is this possible? Thanks!
Edited by googlebot on 18-01-2010 19:49,
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

Use constants?
0 replies
G
googlebot
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
  • Started this discussions
answered
Senior Member

Quote

PolarFox wrote:
Use constants?

I've been reading up on the subject, but I'm still a little confused.

The example on the PHP.net site is:

define("FOO",     "something");

But how would I define $var as a constant? Like this?

define($var,     "something");

But the value changes, so would I have to define something new, with the value of var, like this?

define($new,     $var);

I'm new to the whole functions scene... Sorry about my ignorance.
0 replies
P
PolarFox
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums
answered
Veteran Member

The constants always defined constantly - eg once.
So, if you need change your VARs in future - you should use globals.

Quote

global $var1, var2;
$var1 = 5;
func(){
global $var1;
$newvar = $var1;
echo $newvar;
}


Something liek this...
0 replies
K
KonickMultimedia
K
Free Online Games: http://www.gamescut.com
  • Member, joined since
  • Contributed 76 posts on the community forums.
  • Started 9 threads in the forums
answered
Member

Thanks, was curious about this too:)
0 replies

Labels

None yet

Statistics

  • Views 0 views
  • Posts 11 posts
  • Votes 0 votes
  • Topic users 5 members

5 participants

A
A
afif 10
  • Member, joined since
  • Contributed 183 posts on the community forums.
  • Started 12 threads in the forums
B
B
Basti 10
[PHP-Fusion Crew Member & Admin from June 2008 - December 2010]

http://basti2web.de - Support Site for my infusions
  • Veteran Member, joined since
  • Contributed 1,099 posts on the community forums.
  • Started 32 threads in the forums
K
K
Free Online Games: http://www.gamescut.com
  • Member, joined since
  • Contributed 76 posts on the community forums.
  • Started 9 threads in the forums
G
G
Visit the new home of the merge between Hacking Vs. Security and Security Override!
My copyright removal has been switched over from HvS to SecurityOverride.
  • Senior Member, joined since
  • Contributed 638 posts on the community forums.
  • Started 28 threads in the forums
  • Started this discussions
P
P
  • Veteran Member, joined since
  • Contributed 1,633 posts on the community forums.
  • Started 29 threads in the forums

Notifications

Track thread

You are not receiving notifications from this thread.

Related Questions

Not yet