Fork me on GitHub

accessing a database table  Bottom

  • Hi there,

    I have created a new database table for a module and I am trying to access it like this:

    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();

    $column = &$pntable['bookmarker_column'];
    $result = $dbconn->Execute("
    SELECT count(*)
    FROM $pntable[bookmarker]");

    It will not access, but if I change the instance to bookmarker to a name of one of the core modules like "comments" for instance, it accesses... shoudl this be working to access my new table or is there some other method in accessing the tables of new mods...

    thanks

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • Can you post the pnTables, just the relevant parts to the database?

    --
    Home Page | Find on Facebook | Follow on Twitter
  • Here is the table:

    #
    # Table structure for table `nuke_bookmarker`
    #

    CREATE TABLE `nuke_bookmarker` (
    `pn_id` int(10) NOT NULL auto_increment,
    `pn_uid` varchar(32) NOT NULL default '',
    `pn_bmname` varchar(32) NOT NULL default '',
    `pn_bmURL` varchar(255) NOT NULL default '',
    PRIMARY KEY (`pn_id`,`pn_id`)
    ) TYPE=MyISAM AUTO_INCREMENT=3 ;

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • Yes it is definite - only core module tables can be accessed this way, i added another module and it would not access either...

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • I would assign the SQL to a variable, and output the SQL and see what you get.

    Have you got a pntables.php file with the appropriate table definintions (this is what Michael was referring to).

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • Yes here is the file:

    function bookmarker_pntables()
    {
    // Initialise table array
    $pntable = array();

    // Get the name for the template item table. This is not necessary
    // but helps in the following statements and keeps them readable
    $bookmarker = pnConfigGetVar('prefix') . '_bookmarker';

    // Set the table name
    $pntable['bookmarker'] = $bookmarker;

    // Set the column names. Note that the array has been formatted
    // on-screen to be very easy to read by a user.
    $pntable['bookmarker_column'] = array('uid' => $bookmarker . '.pn_uid',
    'bmname' => $bookmarker . '.pn_bmname',
    'bmURL' => $bookmarker . '.pn_bmURL');

    // Return the table information
    return $pntable;
    }

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • Can you post the function that you're using to try and get the data? The whole function?

    --
    Home Page | Find on Facebook | Follow on Twitter
  • I ended up getting it to work like this:

    function db_connect()
    {
    $result = pnDBGetConn();
    return $result;
    }

    function add_bm()
    {

    if (!($conn = db_connect()))
    return false;

    $bookmarktable = pnConfigGetVar('prefix') . '_bookmarker';

    // check not a repeat bookmark
    $result = mysql_query("select * from ".$bookmarktable."
    where pn_uid='$username'
    and pn_bmURL='$new_url'");
    }

    I guess it is not the most orthodox of ways, but it works and I did try all the ways from the examples and other modules and it didn't...

    So at least it works lol....

    Regards

    --
    -Lobos
    Professional PHP Framework Services: Concept, Development and Deployment
  • ...as Simon had hinted to: the prefix/table/column info is obtained from the pntables.php file.. if the table and colums were defined in that file you would have been able to use the first approach (ie: $pntable[bookmarker])

    ..either way though.. as long as it works.. hahaha

    -IR
  • i have same problem here, i am trying to access to database but no luck :/

    Everything seems ok but it doesnt work..

    ----------------------------------------------------------

    EDIT: Ok finally I got right query ;)

    Code

    list($dbconn) = pnDBGetConn();
       $sql = "SELECT pn_ttest FROM nuke_testimonials WHERE pn_tid='1' ";
       $result = $dbconn->Execute($sql);
       list($ttest) = $result->fields;

       print_r($ttest);


    This one is worked well..
  • It seems to be an unmentioned note that the pnGetDBTables doesn't pull in the added modules as well.. and from looking at the code it was never meant to (amateur here.. go easy.. ;).



    Based on that thought, we can use the modname_pntables() to populate the field by performing the following steps:
    a) Obtain the foldername for the module from website_prefix_modules (pnConfigGetVar('prefix') . '_modules';).
    b) Perform an include on the module's pntables.php.
    c) Call the module's modname_pntables() and put it in $pntables instead.



    The reason I go through all this vs. just hardcoding is to do as the Postnuke teams wants/needs: stick to the API as closely as possible.



    It took me a while to figure this one out - I hope either
    a) Someone just knows something I don't (please post it! =)
    b) Some future version of Postnuke will have an API function to access module data easier
    c) Someone perhaps update the documentation (pnDBGetTables especially!) with this minor little dilemna and perhaps suggest an alternate method (even a link to this article).

    Cheers!

    Code

    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
    $modtable = pnConfigGetVar('prefix') . '_modules';
    $sql = "SELECT pn_directory FROM $modtable WHERE pn_name='" . $GLOBALS['ModName'] . "'";
    $result = $dbconn->Execute($sql);
    $temp = $result->fields;
    $modfolder=$temp['0'];  // we now have the folder name for the module.

    include_once "modules/" . $modfolder . "/pntables.php";  // we now have access to module_pntables();
    $pntables = modtrack_pntables();
    print_r($pntables);




    edited by: fnor_99, Dec 27, 2006 - 11:55 AM
  • There's an API that will do all that for you, pnModDBInfoLoad if I remember correctly.

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • A tip when working on creating SQL statments. Write your code like this.

    Code

    $dbconn =& pnDBGetConn(true);
        $pntable =& pnDBGetTables();

        $bookNameTable = $pntable['bookName'];
        $bookNameList = &$pntable['book_name_list'];

        $nextId = $dbconn->GenId($bookNameTable);

        $sql = "INSERT INTO $bookNameTable (
                  $bookNameList[book_id], "
    .
                  "$bookNameList[book_name])
                VALUES ( $nextId,'"
    . pnVarPrepForStore($name) . "')";
        //print $sql; die;
        $dbconn->Execute($sql);


    By putting the SQL in a variable, if I am having problems getting the DB to take the statement, I can uncomment the line before the Execute statement. Then try it out in the database directly. I like to use phpMyAdmin for this. Almost always, there is a syntax error, and I can figure it out and change the code.





    edited by: Paustian, Dec 28, 2006 - 03:57 AM
  • Ahh.. Hammerhead found the best solution!

    Do this instead, works like a charm:

    Code

    pnModDBInfoLoad($GLOBALS['ModName']);
    $pntable = pnDBGetTables();


    The original problem - pnDBGetTables didn't obtain the table information for 3rd party modules. pnModDBInfoLoad(Modulename) loads that info.

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