SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with Constants

    I'm having a problem with something I wrote a long time ago that is causing problems when run on a newer version of PHP.

    The problem is this. I have a function that loads language strings from a database the defines them as constants.

    Code:
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            define($row['name'], $row['value']);
        }
    In the database I have things like name field "STR_SITE_MAP" with value field "Site Map", then other values for the other languages.
    This used to create the constants with no problem until now. I've searched around but can't find anything about using a string variable as the name for the constant.

    Anyone got any ideas? These are proper constants that do not change for the duration of the script. I went down this route in the first place as it seemed better the using an array or something (scope issues).

    Any help would be great.

    Cheers

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Slarti42 View Post
    This used to create the constants with no problem until now. I've searched around but can't find anything about using a string variable as the name for the constant.

    Cheers
    What type of problem are you having ? Could you elaborate a little ?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Code:
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            define($row['name'], $row['value']);
        }
    Not absolutely sure, define() in the manual suggests it should be quoted like this:

    Code:
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            define("$row['name']", "$row['value']");
        }
    Not in a position to check it, sorry, but it might be worth a try- and at a loss to explain why an upgrade would cause that error to surface anyways ...

  4. #4
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestion, I'll try quoting them.

    To elaborate on the problem. Basically I have a database table with constant names and values stored. Until now when I've retrieved them from the database and defined them like this I get them parsed as expected when echoed out. So I got "Site Map" where I echoed STR_SITE_MAP for example.

    With the newer version of PHP I now get STR_SITE_MAP output to the screen instead. It seems that the constant is never defined and php assumes it to be a string when it gets echoed.

    Cheers

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you try var_dump'ing $row out, inside the loop.

  6. #6
    SitePoint Enthusiast Slarti42's Avatar
    Join Date
    May 2001
    Location
    UK
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh silly silly me!!

    Having taken half the day to set up a test server with the same configuration as the one where the problem had been reported I found the problem in 5 minutes.

    Thanks kyberfabrikken for reminding me to look at the variable. It didn't contain all the strings at all. The problem was not with the define at all. The problem was with the SQL that pulled in the strings in the first place. The SQL worked fine before but the new server had mysql updated to 4.1 and there were minor errors in the sql statement.

    Thanks for the replies anyway


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
  •