Accessing Dynamically created variables inside function

Hi I've been trying to solve this for days so hopefully someone knows the answer.

At the top of my script I dynamically set a variable like so: $p_id = $_GET['p_id'];

I have then attempted to pass this variable to the following function in three different ways without any luck.

1)

public function Insert_Update($uid,$update,$uploads,**$p_id**) 
{
.....        

$query = mysql_query("INSERT INTO `messages` (message, uid_fk, poster_id,ip,created,uploads) VALUES ('$update', '$uid',
**'$p_id'**, '$ip','$time','$uploads')") or die(mysql_error());....

Interestingly, this approach works for the variable if I change the order of the arguments to ($p_id,$uid,$update,$uploads), however the other three variables become invisible to the function.

2)

public function Insert_Update($uid,$update,$uploads) 
{...

     // ... global **$p_id**;

        $query = mysql_query("INSERT INTO `messages` (message, uid_fk, poster_id,ip,created,uploads) VALUES ('$update', '$uid',
        **'$p_id'**, '$ip','$time','$uploads')") or die(mysql_error());....

3)

public function Insert_Update($uid,$update,$uploads) 
{
     ....
      //  $query = mysql_query("INSERT INTO `messages` (message, uid_fk, poster_id,ip,created,uploads) VALUES ('$update', '$uid',
     '".$_REQUEST[**'p_id'**]."', '$ip','$time','$uploads')") or die(mysql_error());...

No matter what approach I try the function never sees the variable. Any ideas? Thanks

Answers


Rather than just using $p_id = $_GET['p_id'];, ensure that $_GET['p_id'] has a value first. Try the following code:

$p_id = isset($_GET['p_id'])? $_GET['p_id'] : "some default value";

Or, if you want to throw an error if the variable isn't set, try this:

if(isset($_GET['p_id']))
{
    $p_id = $_GET['p_id'];
}
else
{
    echo "Please set 'p_id' and try again.";
    exit();
} 

Also, as a general rule of thumb, avoid using the same variable name in a function as defined outside it, if possible. E.g. in your function try naming it $pid instead of $p_id, because that's the variable used outside your function as well.

EDIT Rather than define $p_id as a constant, then add it to the $GLOBALS array, then pass it in, just ensure that $p_id is set, then pass it in. For example:

$p_id = isset($_GET['p_id'])? $_GET['p_id'] : 2;

public function Insert_Update($uid,$update,$uploads,$pid) 
{
.....        

$query = mysql_query("INSERT INTO `messages` (message, uid_fk, poster_id,ip,created,uploads) VALUES ('$update', '$uid',
'$pid', '$ip','$time','$uploads')") or die(mysql_error());....
}

Insert_Update(1, "some text", "some file", $p_id);

You are just wasting memory by creating a new variable.

$_GET['p_id'] is accessible in ANY scope, so it is automatically visible wherever you are accessing it. This is much easier than trying to pass a regular variable into it.


Need Your Help

How to turn off OpenGL filtering when scaling up textures?

c++ c opengl pixel scaling

I already have these lines in here when I'm loading the texture:

Is there a speed difference between WSGI and FCGI?

python wsgi fastcgi

From the web I've gleaned that WSGI is a CGI for python web development/frameworks. FCGI seems to be a more generalised gateway for a variety of languages. Don't know the performance difference bet...