Zikula: A Flexible Open Source Content Management System
home | forum | international support | contact us

Support Forum

Start ::  Developers Corner ::  Block Development ::  Connect to Database

Moderated by: Support Team

Bottom
Connect to Database

  • Link to this postingPosted: 13.06.2006, 05:41
    Profile
    sae
    rank:
    Helper Helper
    registered:
     January 2005
    Status:
    offline
    last visit:
    12.07.08
    Posts:
    310
    Hello,

    I am trying to create a custom block. What is postnukes function to connect to the database and to de-connect from it?

    thanks
  • Link to this postingPosted: 13.06.2006, 12:14
    Profile Homepage
    mhalbrook
    rank:
    Legend Legend
    registered:
     October 2001
    Status:
    online
    Posts:
    6495
    Look at the first.php in the /Example/pnblocks folder.
  • Link to this postingPosted: 15.07.2006, 23:57
    Profile
    sae
    rank:
    Helper Helper
    registered:
     January 2005
    Status:
    offline
    last visit:
    12.07.08
    Posts:
    310
    I took a look in there but I still confused as to how to connect to the database. I read somewhere that there is a snippet of code like this
    Code
    $dbconn =& pnDBGetConn(true);
  • Link to this postingPosted: 16.07.2006, 20:22
    Profile
    TTH_Erik
    rank:
    Freshman Freshman
    registered:
     July 2006
    Status:
    offline
    last visit:
    16.08.06
    Posts:
    11
    Normally you'd place a functions connecting to the database in the API. If you look at first.php you'll see it calling API functions several times. From line 107 in first.php:
    Code
    // Call the modules API to get the items
        $items = pnModAPIFunc('Example',
                              'user'
                              'getall');

    This will call the Example_userapi_getall() function located in pnuserapi.php. If you open this file and locate the function you'll see that it's creating a connection to the database..
    Code
    $dbconn =& pnDBGetConn(true);

    .. and later using that connection to execute SQL.
    Code
    $result = $dbconn->SelectLimit($sql, $numitems, $startnum-1);

    If you don't need to set a limit you can also execute the SQL like this
    Code
    $result =& $dbconn->Execute($sql);

    When you're done fetching the result you should close the result set before returning the data you were looking for
    Code
    $result->Close();

    -----
    Erik from www.TenTonHammer.com
  • Link to this postingPosted: 16.07.2006, 20:48
    Profile
    sae
    rank:
    Helper Helper
    registered:
     January 2005
    Status:
    offline
    last visit:
    12.07.08
    Posts:
    310
    Thanks for the great info. Is there any advantage to using PostNuke's method of connection to the database over using non-postnuke coding? i.e :
    Code
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');




    edited by: sae, Jul 16, 2006 - 03:49 PM
  • Link to this postingPosted: 16.07.2006, 21:09
    Profile
    TTH_Erik
    rank:
    Freshman Freshman
    registered:
     July 2006
    Status:
    offline
    last visit:
    16.08.06
    Posts:
    11
    It's generally a good idea to use core functions for security reasons, but it's also very practical.

    You should at least not write that code in every function.. just imagine the nightmare if you one day have to change the password for example!

    You could have that code in an own file and include it somehow, but why go through that stuff if you can just call the core function and be done with it? icon_smile

    -----
    Erik from www.TenTonHammer.com

Start ::  Developers Corner ::  Block Development ::  Connect to Database

Main Menu

Extensions Database

Documentation

Development

Login





 


 Log in Problems?
 New User? Sign Up!

Donate to Zikula