【问题标题】:Field names, querying Oracle database with PHP字段名,用PHP查询Oracle数据库
【发布时间】:2017-09-24 18:38:44
【问题描述】:

我在下面得到了这个查询,从数据库返回一个查询并在一个表中显示所有信息,然后查询是用户输入的。

有没有办法让字段名称填充在表格的顶部?否则第一行是原始数据。我正在努力寻找一种方法来做到这一点,所以任何帮助都将不胜感激。

<?php

if ($safeparam1 === null) {
}
else {

$stid = oci_parse($conn, $safeparam1);
$r = oci_execute($stid);

print '<p><input type="button" value="Clear Results" 
onclick=location.href=\'ul_move_query.php\';>  <input type="submit" 
value="Print Results">';
print '<table>';

while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
print '<tr>';
foreach ($row as $item) {
    print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) : 
'').'</td>';
}
print '</tr>';
}
}


?>

【问题讨论】:

    标签: php sql oracle


    【解决方案1】:

    当您获取关联数组时,您可以简单地将数组键输出为标题行:

    $header = false; // a way to track if we've output the header.
    while ($row = oci_fetch_array($stid, OCI_RETURN_NULLS+OCI_ASSOC)) {
        if ($header == false) {
            // this is the first iteration of the while loop so output the header.
            print '<thead><tr>';
            foreach (array_keys($row) as $key) {
                print '<th>'.($key !== null ? htmlentities($key, ENT_QUOTES) :
                        '').'</th>';
            }
            print '</tr></thead>';
    
            $header = true; // make sure we don't output the header again.
        }
    
        // output all the data rows.
        print '<tr>';
        foreach ($row as $item) {
            print '<td>'.($item !== null ? htmlentities($item, ENT_QUOTES) :
                    '').'</td>';
        }
        print '</tr>';
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过查询ALL_TAB_COLUMNS表来获取Oracle中特定表的所有列。

      select COLUMN_NAME from ALL_TAB_COLUMNS where TABLE_NAME='MYTABLE';
      

      【讨论】:

      • 我想我正在寻找更面向 php 的东西,因为每个查询都必须这样做对吗?
      • @JamesPavett php 单独无法做到这一点。如果您尝试从数据库中检索列,则必须查询该表。
      • @JamesPavett 您只需要使用 php 执行上述查询。没有其他办法。
      • 我明白你在说什么,但我不想要所有列名,只想要用户正在查询的那些。
      • 然后,放入 where 条件。
      【解决方案3】:

      OCI8 具有元数据调用,例如 oci_num_fields()oci_field_name()。它们可以像这样使用:

      $s = oci_parse($c, "select * from employees");
      if (!$s) {
          $m = oci_error($c);
          trigger_error('Could not parse statement: '. $m['message'], E_USER_ERROR);
      }
      $r = oci_execute($s);
      if (!$r) {
          $m = oci_error($s);
          trigger_error('Could not execute statement: '. $m['message'], E_USER_ERROR);
      }
      
      echo "<table border='1'>\n";
      $ncols = oci_num_fields($s);
      echo "<tr>\n";
      for ($i = 1; $i <= $ncols; ++$i) {
          $colname = oci_field_name($s, $i);
          echo "  <th><b>".htmlspecialchars($colname,ENT_QUOTES|ENT_SUBSTITUTE)."</b></th>\n";
      }
      echo "</tr>\n";
      
      while (($row = oci_fetch_array($s, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
          echo "<tr>\n";
          foreach ($row as $item) {
              echo "<td>";
              echo $item!==null?htmlspecialchars($item, ENT_QUOTES|ENT_SUBSTITUTE):"&nbsp;";
              echo "</td>\n";
          }
          echo "</tr>\n";
      }
      echo "</table>\n";
      

      【讨论】:

        猜你喜欢
        • 2014-04-24
        • 2014-09-08
        • 2013-08-09
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 1970-01-01
        • 2014-09-24
        • 2012-01-25
        相关资源
        最近更新 更多