Php 如何显示数据库中的表?

Php 如何显示数据库中的表?,php,mysqli,Php,Mysqli,我正在尝试使用PHP将MySQL数据库中的表放到HTML页面中。 我是PHP初学者,我面临着mysqli\u查询函数的问题 这是我的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"); //

我正在尝试使用PHP将MySQL数据库中的表放到HTML页面中。
我是PHP初学者,我面临着
mysqli\u查询
函数的问题

这是我的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 />';
  }
}
//连接到数据库
$host='localhost';
$user='root';
$pass='';
$db='testdb';
$connection=mysqli_connect($host、$user、$pass、$db)或die(“无法连接到db”);
//显示表格
$result=mysqli_query($connection,'SHOW TABLES')或die('cannot SHOW TABLES');
while($tableName=mysqli\u fetch\u row($result)){
$table=$tableName[0];
回显“”,$table“”;
$result2=mysqli_查询($table,'SHOW COLUMNS FROM')或die(“cannot SHOW COLUMNS”);
if(mysqli_num_行($result2)){
回声';
回显“FieldTypeNullKeyDefaultExtra”;
而($row2=mysqli\u fetch\u行($result2)){
回声';
foreach($key=>$value的行2){
回显“”,$value“”;
}
回声';
}
回声“
”; } }
不幸的是,我得到了这个错误:

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


我也尝试过使用
mysql\u query
函数,但我遇到了另一个错误,然后我将其改回
mysqli\u query
函数。

在第二次调用
mysqli\u query
函数时,您传递的是表名而不是连接。试着这样做:

$result2=mysqli_query($connection,“显示$table中的列”)或die(“无法显示列”)


在对
mysqli\u query
函数的第二次调用中,传递的是表名而不是连接。试着这样做:

$result2=mysqli_query($connection,“显示$table中的列”)或die(“无法显示列”)


此代码显示所有表格和表格行。我试着用

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

样本输出:


此代码显示所有表格和表格行。我试着用

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

样本输出:


请阅读
mysqli
手册。特别是函数参数的一部分。可能重复的部分请遵循您的第一个用法,
mysqli\u query($connection,
)。以下是手册:。具体说明:
mixed mysqli\u query(mysqli$link,string$query[,int$resultmode=mysqli\u STORE\u RESULT])
。当我测试时,代码对我来说工作正常。你的连接正确吗?检查数据库名称和其他东西。请阅读
mysqli
手册。特别是函数参数的部分。可能重复使用你的第一次用法,
mysqli\u查询($connection,
。这是手册:
混合mysqli\u查询(mysqli$link,string$query[,int$resultmode=mysqli_STORE_RESULT])
。测试时代码对我来说运行良好。您的连接正确吗?检查数据库名称和其他内容。我尝试过,但现在显示了“死亡消息”:无法显示列。仍然有问题请确保使用双引号
($connection),“从$table中显示列”)
或串联
($connection,'SHOW COLUMNS FROM'.$table)
。请参阅我尝试过,但现在显示的是“死亡消息”:无法显示列。仍然有问题。请确保使用双引号
($connection,“SHOW COLUMNS FROM$table”)
或串联
($connection,'SHOW COLUMNS FROM.$table)
。请参阅