简体   繁体   中英

How do I display tables from a database?

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.

This is my PHP code:

// 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.

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

Sample Output :

在此处输入图片说明

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM