SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you insert global variables?

    I'm just learning about global variables, which I understand replace some other function that has fallen out of favor because of security problems. If I understand correctly, you have to cite every variable mentioned on your page as a global variable once.

    So I thought it would be convenient to just list all the variables - which means every field in my MySQL table, right? - in a single row near the beginning of my script.

    So this is what I came up with for a table withi nine fields. But it doesn't work; it killed one of my functions.

    So how would you write global variables for the variables/fields below?

    <?php
    $db = mysql_connect("localhost", "USERNAME", "PASSWORD");
    mysql_select_db("world",$db);

    if (!$connect)
    {
    errorHandling('Database Error: Unable to connect to the database.'); */

    $ID = $myrow["ID"]; $SCode = $myrow["SCode"]; $NameS = $myrow["NameS"];
    $TypeS = $myrow["TypeS"]; $Capital = $myrow["Capital"]; $NameOrigin = $myrow["NameOrigin"];
    $Nicknames = $myrow["Nicknames"]; $Residents = $myrow["Residents"]; $NCode = $myrow["NCode"];
    [/PHP]

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Erm, I think you're a little confused. I believe you're getting register_globals mixed up with the global arrays ($_GET/$_POST) etc.

    The vars in your script above AREN'T global. Have a look at $_GLOBALS.
    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me go back to square one...

    Would it be a good idea to have this $ID = $_POST['ID']; - whatever it's called - on my page? If so, should I have something similar for all the other fields in my MySQL table?

    For example, if my table has three columns, named ID, Names and Types, should I add $ID = $_POST['ID'], $Names = $_POST['Names'] and $Types = $_POST['Types']

    If so, how and where do I add it? The following attempt was rejected...

    $ID = $_POST['ID'], $Names = $_POST['Names'], $Types = $_POST['Types']

    It works when I replace the commas with semicolons; that is, it doesn't generate an immediate error message - the function I'm trying to fix still doesn't work.

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    Would it be a good idea to have this $ID = $_POST['ID']; - whatever it's called - on my page?
    It's not necessarily a bad idea to do so, if that's what you're asking. It's really a matter of personal preference. I prefer not to because (a) I like to know where my variables are coming from and (b) You're using up memory every time you create a new variable.

    Quote Originally Posted by geosite
    For example, if my table has three columns, named ID, Names and Types, should I add $ID = $_POST['ID'], $Names = $_POST['Names'] and $Types = $_POST['Types']
    I don't see why you would want to do that. Your query results won't be coming from the $_POST array; they'll be coming from whatever variable you use in conjunction with mysql_fetch_*(). Most people use $row, as in this short example:
    PHP Code:
     while ($row mysql_fetch_array($result))
     {
         
    // do something...
     


  5. #5
    SitePoint Zealot colinr's Avatar
    Join Date
    Aug 2003
    Location
    san francisco, ca
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't miss the mother of all php posts....
    http://www.sitepoint.com/forums/show...threadid=54074
    Web Slave

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by colinr
    don't miss the mother of all php posts....
    http://www.sitepoint.com/forums/show...threadid=54074
    I just added a link on my website. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •