Zum Inhalt springen

Probleme mit JOIN

Erstellt am 19. April 2011 · 3 Antworten · letzte Antwort am 19. April 2011

Tags: Frage

lil-trick ·

Hallo,

hab ein Problem mit INNER JOIN - er gibt mir nichts aus 🙁

//DatensÀtze holen
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery(
'a.uid, a.title',
'tx_item_produkt a 
INNER JOIN tx_item_produkt_category_mm ac ON (ac.uid_local=a.uid) 
INNER JOIN tx_item_Cat c ON (c.uid=ac.uid_foreign)',
'hidden=0 and deleted=0',
$groupBy='',
$orderBy= '',
$limit='');

while($row=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
$markerArray['###UID###']= $row['uid'];
$markerArray['###TITLE###']= $row['title'];
$liste .= $this->cObj->substituteMarkerArrayCached($singlerow,$markerArray);
}

hab das Ganze aber in einem PHP Script nachgebaut und da funktionierts

$sql = "SELECT a.uid, a.title
FROM tx_item_produkt a
INNER JOIN tx_item_produkt_category_mm ac ON ( ac.uid_local = a.uid )
INNER JOIN tx_item_Cat c ON ( c.uid = ac.uid_foreign )";

$abfrage = mysqli_query($verbindung, $sql);

while($articel = mysqli_fetch_assoc($abfrage)){
echo "<tr>";
echo "<td>$articel[uid] ja</td>";
echo "<td>$articel[title] </td>";
echo "<td>$articel[category]</td>";
echo "</tr>";
}
echo "</table>";
mysqli_free_result();

kann mir einer sagen warum 😃

danke

Julian.​Hofmann ·

Hallo.
Glaube, Du hast in Deinem nachbau den Teil vergessen, der das leere Result - bzw. genauer, den Fehler - verursacht ausgelassen.

Du ĂŒbergibst an die exec_SELECTquery() die WHERE-Clause 'hidden=0 and deleted=0', hast aber innerhalb Deines FROM drei Tabellen. Welche soll hier angesprochen werden?

//DatensÀtze holen
$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery(
'a.uid, a.title',
'tx_item_produkt a 
INNER JOIN tx_item_produkt_category_mm ac ON (ac.uid_local=a.uid) 
INNER JOIN tx_item_Cat c ON (c.uid=ac.uid_foreign)',
'tx_item_produkt.hidden=0 AND tx_item_produkt.deleted=0 AND tx_item_Cat.hidden=0 AND tx_item_Cat.deleted=0' ,
$groupBy='',
$orderBy= '',
$limit='');

if ($res) { 
  while($row=$GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
    $markerArray['###UID###']= $row['uid'];
    $markerArray['###TITLE###']= $row['title'];
    $liste .= $this->cObj->substituteMarkerArrayCached($singlerow,$markerArray);
  }
  $GLOBALS['TYPO3_DB']->sql_free_result($res);
} else {
  echo "Da ging was schief...";
}

Viele GrĂŒĂŸe
Julian

lil-trick ·

da wÀre ich nicht drauf gekommen danke Julian.

Ich möchte jetzt aber zusĂ€tzlich noch ĂŒber eine Variable $auswahl = '1,2,3,4 usw.' festelegen können welche Kategorien angezeigt werden - in meinem test PHP funktioniert das Ganze in typo wieder nicht 🙁

//DatensÀtze holen

$res=$GLOBALS['TYPO3_DB']->exec_SELECTquery(
'a.uid AS uid_local, a.title, c.cat_cat AS category',
'tx_item_produkt a 
INNER JOIN tx_item_produkt_category_mm ac ON (ac.uid_local=a.uid) 
INNER JOIN tx_item_Cat c ON (c.uid=ac.uid_foreign)',
'a.hidden=0 and a.deleted=0 and c.hidden=0 and c.deleted=0',
$groupBy='',
$orderBy= '',
$limit='');

if ($auswahl == '') {
 }else {
$res .='WHERE ac.uid_foreign IN ('.$auswahl.') GROUP BY a.uid';
}
Julian.​Hofmann ·

In $res hast Du einen MySQL result pointer, keinen String mehr. D.h. hier ist es zu spÀt, noch an der Query was drehen zu wollen.

Schau Dir am besten mal die Doku zu exec_SELECTquery() - evtl. auch direkt im PHP-code. Damit wird IMHO deutlicher, wo/wie man an den Queries drehen kann.