如何显示数据库中的表?

时间:2021-01-06 19:30:01

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将MySQL数据库中的表放到一个HTML页面中。我是PHP初学者,我面临mysqli_query函数的一个问题。

This is my PHP code:

这是我的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:

不幸的是,我犯了这个错误:

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

警告:mysqli_query()期望参数1是mysqli,字符串用C:\xampp\htdocs\测试\showtables表示。第26行中的php不能显示列。

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.

我还尝试了mysql_query函数,但是我遇到了另一个错误,然后我将它更改为mysqli_query函数。

Any help would be greatly appreciated, thank you in advance.

如有任何帮助,我们将不胜感激。

2 个解决方案

#1


2  

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 />';
        }
    }
?>

Sample Output :

样例输出:

如何显示数据库中的表?

#2


2  

In the second call to the mysqli_query function, you're passing in the table name instead of the connection. Try something like this:

在对mysqli_query函数的第二个调用中,传递的是表名而不是连接。试试这样:

$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");

$result2 = mysqli_query($connection,“显示来自$table的列”)或die(“不能显示列”);

http://php.net/manual/en/mysqli.query.php

http://php.net/manual/en/mysqli.query.php

#1


2  

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 />';
        }
    }
?>

Sample Output :

样例输出:

如何显示数据库中的表?

#2


2  

In the second call to the mysqli_query function, you're passing in the table name instead of the connection. Try something like this:

在对mysqli_query函数的第二个调用中,传递的是表名而不是连接。试试这样:

$result2 = mysqli_query($connection, "SHOW COLUMNS FROM $table") or die("cannot show columns");

$result2 = mysqli_query($connection,“显示来自$table的列”)或die(“不能显示列”);

http://php.net/manual/en/mysqli.query.php

http://php.net/manual/en/mysqli.query.php