Tachchen zusammen,
ich habe ein ziemlich blödes Problem mit Datenbankabfrage in einer eID-Klasse. Ich will über jQuery einen AJAX-Request machen und in der entsprechenden Datei in die DB schreiben. Dabei habe ich sowohl das $GLOBALS['TYPO3_DB'] als auch das in Kaspers Demoext (whiteboard) verwendete $TYPO3_DB verwendet. Beim ersteren liefert jede Abfrage einfach NULL, bei letzteren gibt es die Methode nicht. Hat jemand ein sinnvolles Beispiel, wie ich hier auf die DB zugreifen kann?
<?php
if (!defined ('PATH_typo3conf')) die ('Could not access this script directly!');
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_sffebookmark_ajax extends tslib_pibase {
function main($data){
$feUserObj = tslib_eidtools::initFeUser(); // Initialize FE user object
$GLOBALS['TSFE']->fe_user = $feUserObj;
tslib_eidtools::connectDB(); //Connect to database
// ...
$user = $GLOBALS['TSFE']->fe_user->user['uid'];
$table = 'tx_sffebookmark_entries';
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'*',
$table,
'feuser = '.intval($user).' AND url = '.$GLOBALS['TYPO3_DB']->fullQuoteStr($data['url'], $table).' AND pagetype = '.intval($data['pagetype'])
);
if($GLOBALS['TYPO3_DB']->sql_num_rows($res) > 0) {
$a[0]['msg'] = 'not added';
} else {
$fields_value = array(
'feuser' => intval($user),
'url' => $GLOBALS['TSFE']->fullQuoteStr($data['url'], $table),
'pagetype' => intval($data['pagetype'])
);
$GLOBALS['TSFE']->exec_INSERTquery($table,$fields_values,$no_quote_fields=FALSE);
$res = $GLOBALS['TSFE']->INSERTquery($table,$fields_values,$no_quote_fields=FALSE);
$a[0]['msg'] = $res;
}
//$GLOBALS['TYPO3_DB']->sql_error()
echo json_encode($a);
//return $a;
}
}
$output = t3lib_div::makeInstance('tx_sffebookmark_ajax');
$output->main($GLOBALS["_GET"]);
?>
Ach ja: TYPO3 4.3.3
Beispiel aus der wt_directory:
<?php
#...
require_once(PATH_t3lib . 'class.t3lib_befunc.php');
require_once(PATH_t3lib . 'stddb/tables.php');
require_once(t3lib_extMgm::extPath('cms', 'ext_tables.php'));
require_once(PATH_tslib.'class.tslib_pibase.php');
class tx_wtdirectory_autocomplete extends tslib_pibase {
var $extKey = 'wt_directory'; // Extension key
var $limit = 100; // limit for db query
// Function main() to set email receiver in session for powermail (if form is rendered in powermail)
function main() {
// config
tslib_eidtools::connectDB();
$this->pid = htmlentities(t3lib_div::_GP('pid')); // GET param
$this->cat = htmlentities(t3lib_div::_GP('cat')); // GET param
$this->field = htmlentities(t3lib_div::_GP('field')); // GET param
$this->search = htmlentities(trim(t3lib_div::_GP('search'))); // GET param
if (empty($this->field)) return $this->wrapToList($this->wrapToList('ERROR: No database field given'), 'ul'); // check if field was given otherwise return error
$this->content = '';
// Let's go
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery ( // DB query
'tt_address.' . $this->field,
'tt_address LEFT JOIN tt_address_group_mm on (tt_address.uid = tt_address_group_mm.uid_local) LEFT JOIN tt_address_group on (tt_address_group_mm.uid_foreign = tt_address_group.uid)',
$this->whereClause() . t3lib_BEfunc::deleteClause('tt_address') . t3lib_BEfunc::BEenableFields('tt_address'),
$this->query['groupby'] = 'tt_address.uid, tt_address.' . $this->field,
$this->query['orderby'] = 'tt_address.' . $this->field,
$this->query['limit'] = $this->limit
);
if ($res) { // If there is a result
while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) { // One loop for every tt_address entry
$this->content .= $this->wrapToList($row[$this->field]);
}
}
$this->content = $this->wrapToList($this->content, 'ul');
return $this->content;
}
#...
?>
Hi Alex,
danke für die Hilfe. Leider führt das ganze bei mir zu keinem Ergebnis. :-(
Mit "tslib_eidtools::connectDB();" sollte ja eigentlich das Objekt $GLOBALS['TYPO3_DB'] vollständig vorhanden sein. Ein print_r auf $GLOBALS['TYPO3_DB'] zeigt ja dann auch, dass die Verbindung zur DB existiert.
Was mich jetzt nur verwirrt, ist die Tatsache, dass
$res = $GLOBALS['TYPO3_DB']->INSERTquery($table,$fields_values,$no_quote_fields=FALSE);
auch NULL zurückgibt. Dabei sollte hier nur das Query zurückgeliefert werden. Aber nix.
Datenbank ist da. Verbindung ist aufgebaut. Tabelle vorhanden. Felder auch.
Ich checks ned! 😢
Oh Sch...,
mal wieder so ein typischer Mischa. ;-) Einmal vertippt im Variablen-Namen und nix geht. So ein Dreck.
Aber danke für die Hilfe.