Jump to content

Dynamic Variable Name Issue


brokencode

Recommended Posts

Necessary info:

 

$svar is equal to "DOG"

 

The code that is causing problems for me is:

 

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

$row = mysql_fetch_array($subid);

$svar = $row['sourcevar1'];

$tvar = $_GET[$svar];

print "$svar - $tvar";

 

When loading the code with a url matching: http://www.site.com/?DOG=poodle I would expect the print statement to read:

 

DOG - poodle

 

but instead, the $tvar variable comes out blank and returns:

 

DOG -

 

Thanks for your time and help!

 

 

Link to comment
Share on other sites

The code blindly assumes that your query worked and returned a row from your database. Since you have no error checking logic in your code to make sure you got a value from your query, there is no guarantee that $_GET[$svar] will produce anything.

 

 

Link to comment
Share on other sites

Where is the variable $tsid defined?

 

$tsid is defined by a separate $_GET variable. In actuallity there is another variable in the url ($tsid=3)

 

If you put

<?php
echo '<pre>' . print_r($_GET,true) . '</pre>';
?>

just before

<?php
$tvar = $_GET[$svar];
?>

what prints?

 

Ken

 

I modified the code with the echo line you suggested:

 

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

$row = mysql_fetch_array($subid);

$svar = $row['sourcevar1'];

echo '<pre>' . print_r($_GET,true) . '</pre>';

$tvar = $_GET[$svar];

print "$svar - $tvar";

 

The code now prints the following:

 

Array

(

    [tsid] => 3

    [OVKEY] => hello

)

 

OKVEY -

 

Thanks for your help guys, don't know what I would do without forums like this and people like you. And also, as I'm sure you noticed my inital post replaced OVKEY with DOG

 

 

Link to comment
Share on other sites

If $tsid is comming from the url you'll need to grab this values using $_GET['tsid'].

// check to to see if tsid set in the url and ensure it holds a numeric value
if(isset($_GET['tsid']) && is_numeric($_GET['tsid']))
{
    // grab tsid from the url
    $tsid = (int) $_GET['tsid'];

    $subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");
    $row = mysql_fetch_array($subid);
    $svar = $row['sourcevar1'];
    echo '<pre>' . print_r($_GET,true) . '</pre>';
    $tvar = $_GET[$svar];
    print "$svar - $tvar";
}

Link to comment
Share on other sites

Sorry, I should have copied more of the function to begin with and not altered anything:

 

I am pulling the $tsid from $_GET['tsid'] and using it's value to pull $row['sourcevar1'] (which is OVKEY)

 

The url is http://www.site.com/?tsid=3&OVKEY=hello

 

The code I have at this point is:

 

///// GET SITE ID

if(isset($_GET['tsid']) && is_numeric($_GET['tsid'])){

$tsid = (int) $_GET['tsid'];

 

///// GET SUBID1 FOR ABOVE TSID

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

$row = mysql_fetch_array($subid);

$svar = $row['sourcevar1'];

echo '<pre>' . print_r($_GET,true) . '</pre>';

$tvar = $_GET[$svar];

print "$svar - $tvar";

}

 

 

 

And the code prints the same thus far:

 

Array

(

    [tsid] => 3

    [OVKEY] => hello

)

 

OKVEY -

Link to comment
Share on other sites

Make sure your query is actually returning a result.

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

// check to see if the query ran fine and results have been returned
if($subid && mysql_num_rows($subid) > 0)
{
    $row = mysql_fetch_array($subid);
    $svar = $row['sourcevar1'];
    echo '<pre>' . print_r($_GET,true) . '</pre>';
    $tvar = $_GET[$svar];
    print "$svar - $tvar";
}
// something is wrong or there was no results returned
else
{
    // check that no error codes where returned
    if(!mysql_errno())
    {
        echo 'No results where returned';
    }
    // Error code returned, lets find out why
    else
    {
        echo 'There is an error! Which is... ' . mysql_error();
    }
}

 

Also ensure you are connected to mysql too.

Link to comment
Share on other sites

Make sure your query is actually returning a result.

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

// check to see if the query ran fine and results have been returned
if($subid && mysql_num_rows($subid) > 0)
{
    $row = mysql_fetch_array($subid);
    $svar = $row['sourcevar1'];
    echo '<pre>' . print_r($_GET,true) . '</pre>';
    $tvar = $_GET[$svar];
    print "$svar - $tvar";
}
// something is wrong or there was no results returned
else
{
    // check that no error codes where returned
    if(!mysql_errno())
    {
        echo 'No results where returned';
    }
    // Error code returned, lets find out why
    else
    {
        echo 'There is an error! Which is... ' . mysql_error();
    }
}

 

Also ensure you are connected to mysql too.

 

I altered my code to read:

 

$tsid = $_GET['tsid'];

 

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

// check to see if the query ran fine and results have been returned

if($subid && mysql_num_rows($subid) > 0){

    $row = mysql_fetch_array($subid);

    $svar = $row['sourcevar1'];

    echo '<pre>' . print_r($_GET,true) . '</pre>';

    $tvar = $_GET[$svar];

    print "$svar - $tvar";

}

// something is wrong or there was no results returned

else{

    // check that no error codes where returned

    if(!mysql_errno())

        echo 'No results';

    // Error code returned, lets find out why

    else

        echo 'There is an error! Which is... ' . mysql_error();

}

 

 

It is still printing:

 

Array

(

    [tsid] => 3

    [OVKEY] => hello

)

 

OKVEY -

 

 

 

Thanks for your continued help.

Link to comment
Share on other sites

I'm going to guess that you have some non-printing characters as part of your data and even though it displays as expected, it does not actually match the $_GET variable name.

 

Use the following to see exactly what $svar might contain -

var_dump($svar);

Link to comment
Share on other sites

I added the dump command - here is what the code looks like:

 

$tsid = $_GET['tsid'];

 

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

// check to see if the query ran fine and results have been returned

if($subid && mysql_num_rows($subid) > 0){

    $row = mysql_fetch_array($subid);

    $svar = $row['sourcevar1'];

    echo '<pre>' . print_r($_GET,true) . '</pre>';

    $tvar = $_GET[$svar];

    print "$svar - $tvar<br />";

    var_dump($svar);

}

// something is wrong or there was no results returned

else{

    // check that no error codes where returned

    if(!mysql_errno())

        echo 'No results';

    // Error code returned, lets find out why

    else

        echo 'There is an error! Which is... ' . mysql_error();

}

 

The printed result is:

 

Array

(

    [tsid] => 3

    [OVKEY] => hello

)

 

OKVEY -

string(5) "OKVEY"

Link to comment
Share on other sites

Okay, changed the code to the following:

 

$tsid = $_GET['tsid'];

 

$subid = mysql_query("SELECT sourcevar1 FROM sources WHERE id='$tsid'");

// check to see if the query ran fine and results have been returned

if($subid && mysql_num_rows($subid) > 0){

    $row = mysql_fetch_array($subid);

    $svar = $row['sourcevar1'];

    echo '<pre>' . print_r($_GET,true) . '</pre>';

    $tvar = $_GET[$svar];

    print "$svar - $tvar<br />";

    var_dump($svar);

    print "<br />";

    var_dump($_GET);

}

// something is wrong or there was no results returned

else{

    // check that no error codes where returned

    if(!mysql_errno())

        echo 'No results';

    // Error code returned, lets find out why

    else

        echo 'There is an error! Which is... ' . mysql_error();

}

 

 

 

And the printed result is:

 

Array

(

    [tsid] => 3

    [OVKEY] => hello

)

 

OKVEY -

string(5) "OKVEY"

array(2) { ["tsid"]=> string(1) "3" ["OVKEY"]=> string(5) "hello" }

 

 

Thank you all for trying to help me through this. It's still not working as expected. But I'm learning alot about error checking!

Link to comment
Share on other sites

OKVEY

 

Thank you - thank you. The only record in the table with a spelling error and I pick it for test purposes.

 

Thank you, thank you, thank you all!

 

I learned alot from this thread, my error checking, among other things, is weak.

Link to comment
Share on other sites

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.