Darkmatter5 Posted January 29, 2009 Share Posted January 29, 2009 I need to get the max record id from a table and store that value into a global variable to be accessed later in the page. So I'm using the following code, but it's not working. <?php include 'library/config.inc.php'; $conn=mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql'); mysql_select_db($dbname); echo "<span class='medium_text'>"; if(isset($_POST['reqsubmit'])) { if(empty($_POST['title'])) { echo "Required field \"title\" wasn't entered!"; } else { $query="SELECT game_id, title FROM games WHERE title='$_POST[title]'"; $result=mysql_query($query) or die(mysql_error()); $count=mysql_num_rows($result); if($count==0) { //insert title data into table //$insert_title="INSERT INTO games (title) VALUES ($_POST[title])"; //mysql_query($insert_title) or die(mysql_error()); //get new game id $query="SELECT MAX(game_id) as game_id FROM games"; $result=mysql_query($query) or die(mysql_error()); $GLOBALS['game_id']=mysql_fetch_array($result); //handling dropdown lists //$insert_genre="INSERT INTO game_genres (game_id, genre_id) VALUES ($game[game_id],$_POST[genre])"; //mysql_query($insert_genre) or die(mysql_error()); print_r($GLOBALS); } } } elseif(isset($_POST['optsubmit'])) { echo "optsubmit"; } echo "</span>"; mysql_close($conn); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142987-help-with-global-variables/ Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 just use: list($game_id) = mysql_fetch_array($result); then it will be in the variable $game_id edit: I used list() because mysql_fetch_array() returns an array. list() assigns the first element in the array to the variable $game_id Quote Link to comment https://forums.phpfreaks.com/topic/142987-help-with-global-variables/#findComment-749783 Share on other sites More sharing options...
Darkmatter5 Posted January 29, 2009 Author Share Posted January 29, 2009 But this only makes the variable $game_id available during that run through the code right? I need to later recall that variable while on this page, if the 'optsubmit' button is pressed. That code is in the elseif after this if. The code will use the game_id variable to insert data into the database. Once the if is run through and $game_id is created with list and the php is exited, the $game_id variable is no longer available right and would need to be recreated? I thought about passing the variable through the URL, but I hate cluttering the URL up, I'd like the variable to be hidden. Am I going about this the right way? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142987-help-with-global-variables/#findComment-749817 Share on other sites More sharing options...
rhodesa Posted January 29, 2009 Share Posted January 29, 2009 oh...global won't solve that problem...either put it in the URL or in a SESSION variable Quote Link to comment https://forums.phpfreaks.com/topic/142987-help-with-global-variables/#findComment-749834 Share on other sites More sharing options...
.josh Posted January 29, 2009 Share Posted January 29, 2009 Since your variable is created outside of a method or function, it is accessible everywhere else on your page, except for inside a method or function. If you want to use it inside a class or function, pass it as an argument to the method or function. Conversely, if you create a variable inside a method or function and want to use it outside of that method or function, assign the method/function to a variable and return the value inside the method/function; it will be assigned to that variable. If you are wanting your data to persist from one script to another, assign it to a session variable, or store it in a database, flatfile or cookie. In short, making variables global is generally bad practice, because if you make a variable without scope, it makes it harder to track what is happening to it throughout your code. Quote Link to comment https://forums.phpfreaks.com/topic/142987-help-with-global-variables/#findComment-749848 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.