brokencode Posted July 6, 2010 Share Posted July 6, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/ Share on other sites More sharing options...
wildteen88 Posted July 6, 2010 Share Posted July 6, 2010 Where is the variable $tsid defined? Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1081999 Share on other sites More sharing options...
kenrbnsn Posted July 6, 2010 Share Posted July 6, 2010 If you put <?php echo '<pre>' . print_r($_GET,true) . '</pre>'; ?> just before <?php $tvar = $_GET[$svar]; ?> what prints? Ken Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082000 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082007 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082008 Share on other sites More sharing options...
wildteen88 Posted July 6, 2010 Share Posted July 6, 2010 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082009 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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 - Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082011 Share on other sites More sharing options...
wildteen88 Posted July 6, 2010 Share Posted July 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082018 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082021 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082041 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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" Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082048 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 Looks OK (the length matches the visible characters.) How about - var_dump($_GET); Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082068 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082076 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2010 Share Posted July 6, 2010 I spy a spelling error. Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082082 Share on other sites More sharing options...
brokencode Posted July 6, 2010 Author Share Posted July 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/206908-dynamic-variable-name-issue/#findComment-1082083 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.