Oracle XE per php abfragen - mehrere Tabellen?

Horschdt

Grünschnabel
Hallo,

Ich hab eine oracle 10g XE mit mehreren Tabellen, eine contact wo Name, Nachname usw. drinsteht und weitere Tabellen wie Addressen, Telefonnummern etc.

Nun brauch ich noch eine vernünftige Seite, die mir die ganzen Daten in einer Tabelle anzeigt (am besten noch mit editiermöglichkeit, aber das kommt später).
MIt dem tool php-Generator kann man sich in 5 sekunden so etwas ähnliches 'basteln', nur kann der eben nur eine Tabelle auf einer Seite anzeigen, meine Versuche den code auf eine sql abfrage über mehrere tabellen zu erweitern tragen jedoch keine Früchte und wenn ich versuche den code von null zu schreiben stoss ich auf eine vielzahl von problemen - hab bisher noch fast nichts mit php am Hut gehabt.

hier der code von dem php Generator:
PHP:
<? session_start(); ?>
<html>
<head>
<title>XE -- HR.X_CONTACT</title>
<meta name="generator" content="text/html">
<style type="text/css">
  body {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .bd {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .tbl {
    background-color: #FFFFFF;
  }
  a:link { 
    color: #FF0000;
    font-family: Arial;
    font-size: 12px;
  }
  a:active { 
    color: #0000FF;
    font-family: Arial;
    font-size: 12px;
  }
  a:visited { 
    color: #800080;
    font-family: Arial;
    font-size: 12px;
  }
  .hr {
    background-color: #808080;
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:link {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:active {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  a.hr:visited {
    color: #FFFFFF;
    font-family: Arial;
    font-size: 12px;
  }
  .dr {
    background-color: #FFFFFF;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
  .sr {
    background-color: #EEEEEE;
    color: #000000;
    font-family: Arial;
    font-size: 12px;
  }
</style>
</head>
<body>

<table width="100%">
<tr>

<td width="10%" valign="top">
<li><a href="query_1.php?a=reset">query_1</a>
</td>
<td width="5%">
</td>
<td bgcolor="#e0e0e0">
</td>
<td width="5%">
</td>
<td width="80%" valign="top">
<?
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  switch ($a) {
    case "view":
      viewrec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;

  ocilogoff($conn);
?>
</td></tr></table>

</body>
</html>

<? function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }

  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {for ($i = 1; $i <= $startrec; ocifetchassoc($res), $i++);}
  $reccount = min($showrecs * $page, $count);
  $fields = array(
    "CONTACTINSTRUCTIONS" => "CONTACTINSTRUCTIONS",
    "ORGANISATIONNAME" => "ORGANISATIONNAME",
    "POSITIONNAME" => "POSITIONNAME",
    "FAMILYNAME" => "FAMILYNAME",
    "INDIVIDUALNAME" => "INDIVIDUALNAME",
    "HOURSOFSERVICE" => "HOURSOFSERVICE");
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr><td>Table: HR.X_CONTACT</td></tr>
<tr><td>Anzeige: <? echo $startrec + 1 ?> - <? echo $reccount ?> von <? echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="HR_X_CONTACT.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>eigener Filter</b>&nbsp;</td>
<td><input type="text" name="filter" value="<? echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">alle Felder</option>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
    if ($val == $filterfield) {$selstr = " selected"; } else {$selstr = ""; }
?>
<option value="<? echo $val ?>"<? echo $selstr ?>><? echo htmlspecialchars($caption) ?></option>
<? } ?>
</select></td>
<td><input type="checkbox" name="wholeonly"<? echo $checkstr ?>>nur ganze Wörter</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Filter anwenden"></td>
<td><a href="HR_X_CONTACT.php?a=reset">Filter zurücksetzen</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<? showpagenav($page, $pagecount); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
?>
<td class="hr"><a class="hr" href="HR_X_CONTACT.php?order=<? echo $val ?>&type=<? echo $ordtypestr ?>"><? echo htmlspecialchars($caption) ?></a></td>
<? } ?>
<td class="hr">&nbsp;</td>
</tr>
<?
  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = ocifetchassoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
?>
<td class="<? echo $style ?>"><? echo htmlspecialchars($row[$val]) ?></td>
<? } ?>
<td class="<? echo $style ?>"><a href="HR_X_CONTACT.php?a=view&recid=<? echo $i ?>">View</a></td>
</tr>
<?
  }
  ocifreestatement($res);
?>
</table>
<br>
<? showpagenav($page, $pagecount); ?>
<? } ?>

<? function showrow($row)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><? echo htmlspecialchars("ID")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["ID"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("CONTACTINSTRUCTIONS")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["CONTACTINSTRUCTIONS"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ORGANISATIONNAME")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["ORGANISATIONNAME"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("POSITIONNAME")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["POSITIONNAME"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("FAMILYNAME")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["FAMILYNAME"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("INDIVIDUALNAME")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["INDIVIDUALNAME"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("HOURSOFSERVICE")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["HOURSOFSERVICE"]) ?></td>
</tr>
</table>
<? } ?>

<? function showpagenav($page, $pagecount)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<? if ($page > 1) { ?>
<td><a href="HR_X_CONTACT.php?page=<? echo $page - 1 ?>">&lt;&lt;&nbsp;zurück</a>&nbsp;</td>
<? } ?>
<?
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><? echo $j ?></b></td>
<? } else { ?>
<td><a href="HR_X_CONTACT.php?page=<? echo $j ?>"><? echo $j ?></a></td>
<? } } } else { ?>
<td><a href="HR_X_CONTACT.php?page=<? echo $startpage ?>"><? echo $startpage ."..." .$count ?></a></td>
<? } } } ?>
<? if ($page < $pagecount) { ?>
<td>&nbsp;<a href="HR_X_CONTACT.php?page=<? echo $page + 1 ?>">vor&nbsp;&gt;&gt;</a>&nbsp;</td>
<? } ?>
</tr>
</table>
<? } ?>

<? function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="HR_X_CONTACT.php">Index Seite</a></td>
<? if ($recid > 0) { ?>
<td><a href="HR_X_CONTACT.php?a=<? echo $a ?>&recid=<? echo $recid - 1 ?>">vorheriger Eintrag</a></td>
<? } if ($recid < $count) { ?>
<td><a href="HR_X_CONTACT.php?a=<? echo $a ?>&recid=<? echo $recid + 1 ?>">nächster Eintrag</a></td>
<? } ?>
</tr>
</table>
<hr size="1" noshade>
<? } ?>


<? function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  for ($i = 1; $i <= $recid; ocifetchassoc($res), $i++);
  $row = ocifetchassoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<? showrow($row) ?>
<?
  ocifreestatement($res);
} ?>

<? function connect()
{
  $conn = ocilogon("HR", "hr", "XE");
  return $conn;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select ID, CONTACTINSTRUCTIONS, ORGANISATIONNAME, POSITIONNAME, FAMILYNAME, INDIVIDUALNAME, HOURSOFSERVICE from HR.X_CONTACT";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (CONTACTINSTRUCTIONS like '" .$filterstr ."') or (ORGANISATIONNAME like '" .$filterstr ."') or (POSITIONNAME like '" .$filterstr ."') or (FAMILYNAME like '" .$filterstr ."') or (INDIVIDUALNAME like '" .$filterstr ."') or (HOURSOFSERVICE like '" .$filterstr ."')";
  }
  if (isset($order) && $order!='') $sql .= " order by \"" .sqlstr($order) ."\"";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = ociquery($sql);
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select count(*) from HR.X_CONTACT";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " where " .sqlstr($filterfield) ." like '" .$filterstr ."'";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " where (CONTACTINSTRUCTIONS like '" .$filterstr ."') or (ORGANISATIONNAME like '" .$filterstr ."') or (POSITIONNAME like '" .$filterstr ."') or (FAMILYNAME like '" .$filterstr ."') or (INDIVIDUALNAME like '" .$filterstr ."') or (HOURSOFSERVICE like '" .$filterstr ."')";
  }
  $res = ociquery($sql);
  $row = ocifetchassoc($res);
  reset($row);
  return current($row);
}

function ociquery($sql)
{
  global $conn;

  $res = ociparse($conn, $sql);
  ociexecute($res, OCI_DEFAULT) or die(ocierror());
  ocicommit($conn);
  return $res;
}

function ocifetchassoc($res)
{
  $ret_array = array();
  ocifetchinto($res, $ret_array, OCI_ASSOC);
  return $ret_array;
} ?>

wenn ich jetzt versuche das sql statement gegen dieses hier auszutauschen:

PHP:
$sql = "SELECT t1.ID, t1.CONTACTINSTRUCTIONS, t1.ORGANISATIONNAME, t1.POSITIONNAME, t1.FAMILYNAME, t1.INDIVIDUALNAME, t1.HOURSOFSERVICE, t2.VOICE, t2.FACSIMILE, t2.MOBILE, t3.SECTION, t4.LINK, t5.CATEGORY, t6.ELECTRONICMAILADDRESS, t6.ADMINISTRATIVEAREA, t6.COUNTRY, t6.DELIVERYPOINT, t6.CITY
FROM HR.X_CONTACT t1
LEFT JOIN HR.X_TELEPHONE t2 ON t2.ID=t1.ID
LEFT JOIN HR.X_SECTION t3 ON t3.ID=t1.ID
LEFT JOIN HR.X_ONLINERESOURCE t4 ON t4.ID=t1.ID
LEFT JOIN HR.X_CATEGORY t5 ON t5.ID=t1.ID
LEFT JOIN HR.X_ADDRESS t6 ON t6.ID=t1.ID;";

wird das ganze nichts mehr.. falls hier einer eine ahnung hat, wie ich das irgendwie hinbekomme, ich bin für jede Hilfe dankbar.

Gruß,
Alex
 
Zuletzt bearbeitet:
ok, hat sich erledigt.. ich war zu dumm die abfrage richtig zu stellen.. hatte in meiner seite noch ein unsinniges " and t1.ID in (600, 900) " an den dem select stehen.. jetzt funktioniert es.
 
Zurück