【问题标题】:Table extraction from SQL dB从 SQL dB 中提取表
【发布时间】:2016-11-17 04:20:41
【问题描述】:

我已经设置了 wamp 服务器。我已经在数据库测试下上传了一个名为 stud 的表到 phpMyAdmin。现在我想在 Web 浏览器中将其显示为以行作为分隔符的表格。我将以下代码编写为 PHP。当我尝试运行localhost/hw.php时,报错如下:

<html>
    <head>
        <title>mysqli Table Viewer</title>
    </head>
    <body>
        <?php
        $db_host = 'localhost';

        $database = 'test';
        $table = 'stud';

        if (!mysqli_select_db($database))
            die("Can't select database");

        // sending query
        $result = mysqli_query("SELECT * FROM {$table}");
        if (!$result) {
            die("Query to show fields from table failed");
        }

        $fields_num = mysqli_num_fields($result);

        echo "<h1>Table: {$table}</h1>";
        echo "<table border='1'><tr>";

        // printing table headers
        for($i=0; $i<$fields_num; $i++)
        {
            $field = mysqli_fetch_field($result);
            echo "<td>{$field->name}</td>";
        }
        echo "</tr>\n";

        // printing table rows
        while($row = mysqli_fetch_row($result))
        {
            echo "<tr>";

            // $row is array... foreach( .. ) puts every element
            // of $row to $cell variable
            foreach($row as $cell)
                echo "<td>$cell</td>";

            echo "</tr>\n";
        }

        mysqli_free_result($result);
        ?>
    </body>
</html>

如何解决这个错误?

【问题讨论】:

  • 您永远不会执行到数据库的连接。

标签: php html mysqli html-table


【解决方案1】:

在访问表之前,您必须连接到数据库,此连接对于数据库上的所有操作都是必需的。在接下来的代码中,我使用箭头 (// ◄■■■) 指向更改(注意 $cnx 变量):

<html>
<head>
<title>mysqli Table Viewer
</title></head><body>
<?php
$db_host = 'localhost';

$database = 'test';
$table = 'stud';

$cnx = mysqli_connect( $db_host, "root", "", $database ) //◄■■■■■■■■■■■■■■
       or die(mysqli_error($cnx));

if (!mysqli_select_db( $cnx, $database ) )               //◄■■■■■■■■■■■■■■
    die("Can't select database");

// sending query
$result = mysqli_query( $cnx, "SELECT * FROM {$table}" );  //◄■■■■■■■■■■■
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysqli_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
    $field = mysqli_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysqli_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysqli_free_result($result);
?>
</body>
</html>

【讨论】:

  • 亲爱的何塞,这正是我想要的。非常感谢。作为此查询的扩展,如何使标题行加粗并居中对齐。
  • @MelvinKoshy => echo "&lt;table border='1'&gt;&lt;tr style='font-weight:bold;text-align:center'&gt;";echo "&lt;td style='font-weight:bold;text-align:center'&gt;{$field-&gt;name}&lt;/td&gt;";
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-01
相关资源
最近更新 更多