Fork me on GitHub

Weirdness with pnModGetVar  Bottom

Go to page 1 - 2 [+1]:

  • I'm new to postnuke mod development and am having some problems with pnModGetVar. My code seems to work once then returns an array from pnModGetVar on subsequent uses. Here is my code:

    Code

    function footermenu($week){
        $numweeks = pnModGetVar('pnBaseball','numWeeks');
        if (!is_numeric($numweeks)){
        print 'Something dicked up.  $numweeks is ';
        print_r($numweeks);
            return;
        }


    numWeeks is set to 28 in the database. The first time I hit the page the footer prints correctly. The second time I hit the page I get this:

    Something dicked up. $numweeks is Array ( )

    Any ideas what I'm doing wrong? Postnuke version is .750

    Thanks,
    Nick
  • The variable names $numWeeks and $numweeks are different... does that matter at all in the context of the rest of your code? Assuming that nothing is being written when you click the second time, it should still return 28, rather than an array.

    Also, please post these sort of questions in the module development forum. Thanks!

    ;)
  • I don't have a variable called $numWeeks, only $numweeks. numWeeks is the module variable name stored in the database. I have no code that changes this variable. It is set during initialization and only read from, never written to.

    I've looked through the pnModGetVar code and can see when an array might be returned, but that should not apply here.. Any other ideas?

    Nick
  • Right, just checking. No sorry...just pointing out the CAPS because some users might not have realized the difference. Typos are the leading cause of errors...sorry to underestimate your skills.

    I've not had any trouble with the function...but for the most part, I use the pnMod***Var functions to store and retrieve serialized values for a given module (meaning in my own custom jobbies) and then unserialize it after it's grabbed...so "real" arrays never come into play for me, unless I assign the data to an array after it's unserialized. Um..hope that made sense... Sorry...another all-nighter...:|
  • Check what you stored there with pnModSetVar(). You can look at the module_vars table using phpMyAdmin if cannot determine it from your code.

    Frank

    --
    "He is not dangerous, he just wants to play...."
  • I've not actually seen an array returned via this function...is that an odd behaviour, Frank? Now, I'm not guru (yet!) but I think that arrays can't be stored as is in the database...is that true?
  • Actually, if you serialize and unserialize them at the appropriate points, you can. I've done this in various stages of my project tracker module. :)
  • You can store an array if you serialize() it before storing it and unserialize() it after reading it from the database. This is quite usual. And in addition, pnModGetVar() can return an empty array.

    e.g. all pnConfig vars are serialized.

    For bughunting you can use

    Code

    $modvars = pnModGetVar('pnBaseball');
    print_r($modvars);


    This will show all modvars that exist for pnBaseball.

    Frank

    --
    "He is not dangerous, he just wants to play...."
  • Right. The serialize/unserialize functions are designed for that. However, once you serialize an array, it's no longer an array...instead it's then a string...and it only becomes an array again after unserialization...

    The functions work equally well serializing/unserializing entire objects too...just in case that ever comes up...never know when you'll need to store an object...but it's handy.
  • OK guys...I've gotten somewhere on this, although I don't know why what I changed fixed it. I have another block of code that basically does this:

    Code

    $dbconn =& pnDBGetConn(true);
        $sql = "SQL QUERY";
        $result = $dbconn->Execute($sql);
        do STUFF WITH RESULT SET
        $result->Close();
        $dbconn->Close();


    The problem code was the $dbconn->Close(). I removed that line and now the page works fine.

    Question now is, do I not have to close my DB connection? Is there a PN function I need to call instead? Does closing the result set close the connection?

    Nick
  • I've never seen the $dbconn->Close(); used in a module (not to say that I've seen them all though.) But I do know that $result->Close(); does the trick. In any case, I don't believe the $dbconn is left open. This is my logic, based on just my 2 cents really....

    Since $result = $dbconn->Execute($SQL); it would stand to reason that closing the $result automatically closes the $dbconn.

    Frank can probably comment better on this...he's a core developer after all. ;)
  • alarconcepts

    Frank can probably comment better on this...he's a core developer after all. ;)


    icon_rolleyes

    $result->Close() is correct IMHO, $dbconn is a database connection object that can be re-used again and again in the same function. At least this is what I do in pnForum icon_smile and it works.

    Frank

    --
    "He is not dangerous, he just wants to play...."
  • Sorry to dump that in your lap, Frank. But I think you're right...I too reuse the $dbconn resource multiple times over the "course" of a function in cases. And you know what they say..."If it ain't broke..."

    ;)
  • "... it will become standard icon_smile "

    --
    "He is not dangerous, he just wants to play...."
  • :) ... I like the way you work.

Go to page 1 - 2 [+1]:

This list is based on users active over the last 60 minutes.