五、显示数据库、表内容、表结构

1showdatabases.php

<?php
mysql_connect("localhost", "root" ,'123456');
mysql_set_charset("utf8");
$sql = "show databases;";
$result = mysql_query($sql);
if($result === false){
 echo "执行失败";
}
else{

 $fieldCount = mysql_num_fields( $result ); //取得结果集的列数

 echo "<table border='1'>";
 //表头
 echo "<tr>";
 for($i = 0;  $i < $fieldCount; ++$i){
  $fieldName = mysql_field_name($result, $i);//取得第i个列名;
  echo "<th>" . $fieldName . "</th>";
 }
 echo "<th>动作</th>";
 echo "</tr>";

 while( $arr = mysql_fetch_assoc( $result ) ){
  echo "<tr>";
  for($i = 0;  $i < $fieldCount; ++$i){
   $fieldName = mysql_field_name($result, $i);//取得第i个列名;
   echo "<td>" . $arr[$fieldName] . "</td>";
   echo "<td><a href='2show_tables.php?db={$arr[$fieldName]}'>查看表</a></td>";
  }
  echo "</tr>";
 }
 echo "</table>";
}
?>

2show_tables.php

<?php
mysql_connect("localhost", "root" ,'123456');
mysql_set_charset("utf8");
$db = $_GET['db'];
mysql_select_db($db);
$sql = "show tables;";
$result = mysql_query($sql);
if($result === false){
 echo "执行失败" . mysql_error();
}
else{

 $fieldCount = mysql_num_fields( $result ); //取得结果集的列数

 echo "<table border='1'>";
 //表头
 echo "<tr>";
 for($i = 0;  $i < $fieldCount; ++$i){
  $fieldName = mysql_field_name($result, $i);//取得第i个列名;
  echo "<th>" . $fieldName . "</th>";
 }
 echo "<th>动作</th>";
 echo "</tr>";

 while( $arr1 = mysql_fetch_assoc( $result ) ){
  echo "<tr>";
  for($i = 0;  $i < $fieldCount; ++$i){
   $fieldName = mysql_field_name($result, $i);//取得第i个列名;
   echo "<td>" . $arr1[$fieldName] . "</td>";
   echo "<td><a href='3show_data.php?db=$db&tab={$arr1[$fieldName]}'>查看数据</a></td>";
   echo "<td><a href='4show_struct.php?db=$db&tab={$arr1[$fieldName]}'>查看结构</a></td>";
  }
  echo "</tr>";
 }
 echo "</table>";
}
?>

3show_data.php

<?php
mysql_connect("localhost", "root" ,'123456');
mysql_set_charset("utf8");
$db = $_GET['db'];
$tab = $_GET['tab'];
mysql_select_db($db);
$sql = "select * from $tab";
$result = mysql_query($sql);
if($result === false){
 echo "执行失败" . mysql_error();
}
else{

 $fieldCount = mysql_num_fields( $result ); //取得结果集的列数

 echo "<table border='1'>";
 //表头
 echo "<tr>";
 for($i = 0;  $i < $fieldCount; ++$i){
  $fieldName = mysql_field_name($result, $i);//取得第i个列名;
  echo "<th>" . $fieldName . "</th>";
 }
 echo "</tr>";

 while( $rec = mysql_fetch_assoc( $result ) ){
  echo "<tr>";
  for($i = 0;  $i < $fieldCount; ++$i){
   $fieldName = mysql_field_name($result, $i);//取得第i个列名;
   echo "<td>" . $rec[$fieldName] . "</td>";
  }
  echo "</tr>";
 }
 echo "</table>";
}
?>

4show_struct.php

<?php
mysql_connect("localhost", "root" ,'123456');
mysql_set_charset("utf8");

$db = $_GET['db'];
$tab = $_GET['tab'];
mysql_select_db($db);

$sql = "desc $tab";
$result = mysql_query($sql);
if($result === false){
 echo "执行失败" . mysql_error();
}
else{

 $fieldCount = mysql_num_fields( $result ); //取得结果集的列数

 echo "<table border='1'>";
 //表头
 echo "<tr>";
 for($i = 0;  $i < $fieldCount; ++$i){
  $fieldName = mysql_field_name($result, $i);//取得第i个列名;
  echo "<th>" . $fieldName . "</th>";
 }
 echo "</tr>";

 while( $rec = mysql_fetch_assoc( $result ) ){
  echo "<tr>";
  for($i = 0;  $i < $fieldCount; ++$i){
   $fieldName = mysql_field_name($result, $i);//取得第i个列名;
   echo "<td>" . $rec[$fieldName] . "</td>";
  }
  echo "</tr>";
 }
 echo "</table>";
}
?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值