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
- Teb answered »MenuTree Mystery« 23:29h
- dks answered »include a file« 18:22h
- dks answered ».764 -> 1.1.1, Can't add new blocks« 17:33h
- craigh posted »PostCalendar 5.5.0RC1 needs testers and translators« 17:24h
- bartl answered »Upgrading 1.0.2 --> 1.1.1« 15:02h
- Charlie-otb answered »SafeHTML output filter and youtube« 10:02h
- dl7und answered »Adding an "About Us" page« 03:34h
Login
Connect to Database
-
-
- Rank: Legend
- Registered: 31.12.69
- last visit: 30.06.09
- Posts: 6766
-
- Rank: Helper
- Registered: 29.01.05
- last visit: 29.10.08
- Posts: 312
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); -
- Rank: Freshman
- Registered: 10.07.06
- 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 -
- Rank: Helper
- Registered: 29.01.05
- last visit: 29.10.08
- Posts: 312
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 -
- Rank: Freshman
- Registered: 10.07.06
- 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? :)
--
Erik from www.TenTonHammer.com
- Moderated by :
- Support
