如何显示数据库中的表? [英] How do I display tables from a database?

查看:64
本文介绍了如何显示数据库中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP将MySQL数据库中的表放入HTML页面中.
我是PHP的初学者,我遇到了mysqli_query函数的问题.

I'm trying to put the tables from MySQL database in a HTML page using PHP.
I'm beginner in PHP and I face a problem to the mysqli_query function.

这是我的PHP代码:

// connect to the db
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'testdb';
$connection = mysqli_connect($host, $user, $pass, $db) or die("cannot connect to db");

// show the tables
$result = mysqli_query($connection, 'SHOW TABLES') or die ('cannot show    tables');
while($tableName = mysqli_fetch_row($result)) {
  $table = $tableName[0];
  echo '<h3>', $table, '</h3>';
  $result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
  if(mysqli_num_rows($result2)) {
    echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
    echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
    while($row2 = mysqli_fetch_row($result2)) {
      echo '<tr>';
      foreach ($row2 as $key=>$value) {
        echo '<td>',$value, '</td>';
      }
      echo '</tr>';
    }
    echo '</table><br />';
  }
}

不幸的是,我收到此错误:

Unfortunately I get this error:

警告:mysqli_query()期望参数1为mysqli,字符串在 第26行的C:\ xampp \ htdocs \ test \ showtables.php无法显示列.

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\test\showtables.php on line 26, cannot show columns.

我也尝试过使用mysql_query函数,但是遇到另一个错误,然后我将其改回了mysqli_query函数.

I've also tried with the mysql_query function, but I faced another error, I then I changed it back to the mysqli_query function.

推荐答案

此代码向您显示所有表和表行.我尝试了

This code show you all of the tables and tables rows. I try it works

<?PHP
    $host = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'testdb';

    $mysqli = new mysqli($host, $user, $pass, $db);

    //show tables
    $result = $mysqli->query("SHOW TABLES from testdb");
    //print_r($result);
    while($tableName = mysqli_fetch_row($result))
    {
        $table = $tableName[0];
        echo '<h3>' ,$table, '</h3>';
        $result2 = $mysqli->query("SHOW COLUMNS from ".$table.""); //$result2 = mysqli_query($table, 'SHOW COLUMNS FROM') or die("cannot show columns");
        if(mysqli_num_rows($result2))
        {
            echo '<table cellpadding = "0" cellspacing = "0" class "db-table">';
            echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default</th><th>Extra</th></tr>';
            while($row2 = mysqli_fetch_row($result2))
            {
                echo '<tr>';
                foreach ($row2 as $key=>$value)
                {
                    echo '<td>',$value, '</td>';
                }
                echo '</tr>';
            }
            echo '</table><br />';
        }
    }
?>

样本输出:

这篇关于如何显示数据库中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆