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
- craigh responded to »TagIt 3.0 for Zikula« 01:46 PM
- jmvaughn responded to »error when i try to upgrade to the last version of dizkus module (3.1)« 12:05 PM
- localrags responded to »Remove contents of nuke_sc_anticracker from Database« 11:30 AM
- jmvaughn responded to »Shoutit for zikula 1.3?« 09:31 AM
- mdee responded to »Different page content under one template (tpl file) based on URL« 07:17 AM
- espaan responded to »Categories disappear when editing ...« 08. Feb
- eledril responded to »How decrease zikula cpu usage« 08. Feb
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
Connect to Database
-
- Rank: Softmore
- Registered: Jan 29, 2005
- Last visit: Oct 21, 2009
- Posts: 269
-
- Rank: Developer
- Registered: Dec 31, 1969
- Last visit: Jun 01, 2010
- Posts: 6840
Look at the first.php in the /Example/pnblocks folder.
--
Home Page | Find on Facebook | Follow on Twitter
-
- Rank: Softmore
- Registered: Jan 29, 2005
- Last visit: Oct 21, 2009
- Posts: 269
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); -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Aug 15, 2006
- 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(); -
- Rank: Softmore
- Registered: Jan 29, 2005
- Last visit: Oct 21, 2009
- Posts: 269
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
edited by: sae, Jul 16, 2006 - 03:49 PM -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Aug 15, 2006
- 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? :)
- Moderated by:
- Support
