<html lang="zh-cn">
<head>
<meta name="generator"
content="Oracle Test" />
<meta charset="utf-8" />
<meta name="robots" content="all" />
<meta name="author" content="w3school.com.cn" />
<link rel="stylesheet" type="text/css" href="/c5.css" />
<style type="text/css">
.title {
font-weight:bold;
}
td {
width:120px;
padding-left:10px;
}
table {
border: 0.5px solid #778899;
border-collapse:collapse;
border-spacing:0;
padding:0;
}
.db_info {
color:Navy;
background-color: Lavender;
width:660px;
font-weight:bold;
padding:10px 10px 10px 20px;
line-height:25px;
text-align:left;
border-radius:15px;
-moz-border-radius:15px; /* Old Firefox */
}
</style>
<title>Oracle数据库表结构</title>
</head>
<body>
<center>
<?php
/
// 数据库连接配置信息
$ora_host = "";
$ora_port="";
$ora_sid = "";
$ora_username = "";
$ora_password = "";
$charset = "UTF8"; ### zhs16gbk ###
// 连接字符串
$ora_connstr = "(description=(address=(protocol=tcp)
(host=".$ora_host.")(port=".$ora_port."))
(connect_data=(service_name=".$ora_sid.")))";
/
// 测试连接
$conn = oci_connect($ora_username, $ora_password, $ora_connstr,'UTF8');
if (!$conn) {
echo "连接Oracle失败!<br/>";
$e = oci_error();
print htmlentities($e['message']);
exit;
}else{
echo "连接Oracle成功!<br/>";
}
echo "用户:".$ora_username." 服务器:".$ora_host." SID:".$ora_sid."<br/><br/>";
/
// 查询数据库版本
$sql_version='select banner from sys.v_$version';
$stid_sql_version = oci_parse($conn, $sql_version); // 配置 Oracle 语句预备执行
if (!$stid_sql_version) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r_sql_version = oci_execute($stid_sql_version, OCI_DEFAULT); // 执行SQL。OCI_DEFAULT表示不要自动commit
if(!$r_sql_version) {
$e = oci_error($stid_sql_version);
echo htmlentities($e['message']);
exit;
}
/
// 打印执行结果
print "<div class=\"db_info\">";
while($row = oci_fetch_row($stid_sql_version)) {
$ver_info=$row[0];
echo "<cite>".$ver_info."</cite><br/>";
}
print "</div><br/>";
/
// 查询用户下的表数量
$sql_showtables='select table_name from user_tables';
$stid = oci_parse($conn, $sql_showtables); // 配置 Oracle 语句预备执行
if (!$stid) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r = oci_execute($stid, OCI_DEFAULT); // 执行SQL。OCI_DEFAULT表示不要自动commit
if(!$r) {
$e = oci_error($stid);
echo htmlentities($e['message']);
exit;
}
/
// 打印执行结果
while($row = oci_fetch_row($stid)) {
$table_name=$row[0];
echo "<strong>数据表名:".$table_name."</strong>";
/
// 查询表名注释
$tab_comments='select b.comments as 注释 from USER_TAB_COMMENTS b where b.TABLE_NAME=upper(\''.$table_name.'\')';
$stid_tab_comments = oci_parse($conn, $tab_comments); // 配置SQL语句,准备执行
if (!$tab_comments) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r_tab_comments = oci_execute($stid_tab_comments, OCI_DEFAULT); // 执行SQL。OCI_DEFAULT表示不要自动commit
if(!$r_tab_comments) {
$e = oci_error($stid_tab_comments);
echo htmlentities($e['message']);
exit;
}
while($row_tab_comments = oci_fetch_row($stid_tab_comments)) {
print" <strong>备注:$row_tab_comments[0]</strong>";
print"<br/>";
}
/
// 查询TABLE META
$table_meta='select a.column_name,a.data_type,a.data_length,a.NULLABLE
from
user_tab_columns a
where a.TABLE_NAME=upper(\''.$table_name.'\')';
$stid_table_meta = oci_parse($conn, $table_meta); // 配置SQL语句,准备执行
if (!$stid_table_meta) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r_table_meta = oci_execute($stid_table_meta, OCI_DEFAULT); // 执行SQL。OCI_DEFAULT表示不要自动commit
if(!$r_table_meta) {
$e = oci_error($stid_table_meta);
echo htmlentities($e['message']);
exit;
}
/
// 打印数据表结构表格
print '<table border="1">';
print '<tr bgcolor="#6495ED" class="title"><td>字段名</td><td><p>数据类型</td><td>数据长度</td><td>是否可为空</td><td>注释</td></tr>';
while($row_table_meta = oci_fetch_row($stid_table_meta)) {
//&& $row_col_comments = oci_fetch_row($stid_col_comments
print"<tr bgcolor=\"#F0FFFF\"><td>$row_table_meta[0]</td>";
print"<td>$row_table_meta[1]</td>";
print"<td>$row_table_meta[2]</td>";
print"<td>$row_table_meta[3]</td>";
/
// 查询对应的字段注释
$col_comments='select b.comments as 注释 from USER_COL_COMMENTS b where b.column_name=upper(\''.$row_table_meta[0].'\') and b.table_name=upper(\''.$table_name.'\')';
$stid_col_comments = oci_parse($conn, $col_comments); // 配置SQL语句,准备执行
if (!$col_comments) {
$e = oci_error($conn);
print htmlentities($e['message']);
exit;
}
$r_col_comments = oci_execute($stid_col_comments, OCI_DEFAULT); // 执行SQL。OCI_DEFAULT表示不要自动commit
if(!$r_col_comments) {
$e = oci_error($stid_col_comments);
echo htmlentities($e['message']);
exit;
}
while($row_col_comments = oci_fetch_row($stid_col_comments)) {
//print"<td>$row_col_comments[0]</td>";
print"<td>$row_col_comments[0]</td>";
}
/
//print"<td> </td>";
print"</tr>";
}
/
print"</table></br>";
}
// 关闭连接
oci_close($conn);
?>
</center>
</body>
</html>