Jump to content

Recommended Posts

I have a function i am trying to call in a script but i get an undefined variable every time and I don't know why... this is what i have..:

 

<?php
function collect(){
$Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Get);
$Username = $row['Username'];
}

collect();
Echo $Username
?>

 

My error:

Notice: Use of undefined constant collect - assumed 'collect
Notice: Undefined variable: Username in test.php on line 15

 

 

What did i do wrong?

Link to comment
https://forums.phpfreaks.com/topic/141668-solved-help-with-a-function-call/
Share on other sites

Try this:

<?php
function collect(){
$Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Get);
return $row['Username'];
}


echo collect();
?>

Variables inside functions are destroyed after the function is run, but you can return the user name variable...

Variables inside functions are destroyed after the function is run, but you can return the user name variable...

 

Hmm is there no way to load variables in a function to pass them back to use at my own disposal when needs be? Example is shown below:

 

<?php
function collect(){
$Get = mysql_query("SELECT Username,Level from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Get);
$Username = $row['Username'];
$Level = $row['Level'];
}

collect();
Echo $Username;
?>
<br><br>
<p align=right>
<?php
Echo 'Level '.$Level;
?>
</p>
?>

 

Obviously from what you said variables are destroyed so is there any work arounds ?

you can make them global...not the most secure of programming strategies, but it will work

function collect(){
$Get = mysql_query("SELECT Username,Level from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
$row = mysql_fetch_assoc($Get);
global $Username = $row['Username'];
global $Level = $row['Level'];
}

collect();
Echo $Username;

you can make them global

 

Don't do that.

 

Why is that then ? And im assuming if thats the only way and it's not recommended I can't place my query in a function to call variables when needed. Thats a shame, maybe a nice work around will be store php scripts in database and call them with a query. Either that or I'm going too over the top with reducing script sizes hehe

just return the variable (or variables, as an array) and assign the function call to something.

 

function something () {
   $array = array('a','b','c');
   return $array;
}

$foobar = something();

echo $foobar[0]; // output: a
echo $foobar[1]; // output: b
echo $foobar[2]; // output: c

your best bet in this would be to return your $row array ... from collect like so

function collect(){
$Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
return $row = mysql_fetch_assoc($Get);
}
$stuff = collect();
echo $stuff['Username'];

 

Also since you're only searching for one thing in the database, you might as well just use mysql_result

so ....

function collect(){
$Get = mysql_query("SELECT Username from users WHERE UserID='{$_SESSION['UserID']}'")
Or die(mysql_error());
return $theUsername = mysql_result($Get, 0);
}

$thePersonsUsername = collect();
echo $thePersonsUsername

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.