[TYPO3-dev] extra helper files in plugin -> how to access db ?
Dmitry Dulepov
dmitry at typo3.org
Fri Jan 30 12:52:06 CET 2009
Hi!
Henjo Hoeksma wrote:
> The php code code creates the xml data as output using 'echo
> $dom->saveXML();'.
> The content of this XML is coming from the database.
>
> The pi1 class accesses this XML to retrieve the correct data.
> In order to have the correct data in the XML it needs to access the DB.
If it is a TYPO3 database, you can use $GLOBALS['TYPO3_DB'], which is an instance of the t3lib_db class (a simple wrapper around MySQL PHP functions).
> Which I now did by making the connection the normal php way (using
> mysql_connect() ). The variables for the db, user and host are in this
> file too - which I'd like to not have but have it come from within TYPO3...
Never ever use mysql_* functions directly inside TYPO3 because you risk corrupting your databases (any of them). Always use TYPO3 wrapper class. If you need to instantiate this wrapper yourself to connect to another database, it is easy:
$db = t3lib_div::makeInstance('t3lib_DB');
if ($db->sql_pconnect($dbhost, $dbuser, $dbpass)) {
if ($db->sql_select_db($dbname)) {
// You got the connection!
$db->exec_INSERTquery('mytable', array(
'field1' => 'value1',
'field2' => 5
));
}
}
> Hope this is more clear. I am making my first steps in this so I am
> sorry if I am somewhat confusing in my description!
Get my book about extension programming. You will learn much faster than trying it the wrong way ;)
http://www.packtpub.com/typo3-extension-development/
--
Dmitry Dulepov
TYPO3 core team
"Sometimes they go bad. No one knows why" (Cameron, TSCC, "Dungeons&Dragons")
More information about the TYPO3-dev
mailing list