Jump to content

Help with GLOBAL variables


Darkmatter5

Recommended Posts

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);
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

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.