简体   繁体   English

用HTML打印出MySQL表

[英]Printing out MySQL tables in HTML

I am currently using the code below to print out a MySQL table. 我目前正在使用下面的代码来打印出MySQL表。 However, when I am printing multiple rows the format is pretty ugly. 但是,当我打印多行时,格式很难看。 What is a way or command I could create columns similar to the Tab key to make it more aesthetically pleasing? 我可以通过什么方式或命令创建类似于Tab键的列,以使其更美观?

$google= mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
    print "$cvalue\t <tr>";
}

print "<br>";
}

The query is printed using user input. 使用用户输入来打印查询。 using the 使用

<form action="google.php" method="post">

command. 命令。 I am also looking for a way to print this table on the same page as the HTML page that I accept the user input. 我也在寻找一种在与我接受用户输入的HTML页面相同的页面上打印此表的方法。

Your code is pretty good and only needs HTML added to it to make it look nicer. 您的代码相当不错,只需要添加HTML即可使其看起来更好。 Using tables (this has not been tested but it should work): 使用表(尚未经过测试,但应该可以使用):

$google = mysqli_query($query) or die(mysqli_error());

echo '<table>';

while($row = mysqli_fetch_assoc($google)) {
    echo '<tr>';
    foreach($row as $cvalue) {
        print '<td>'.$cvalue.'</td>';
    }
    echo '</tr>';
}

echo '</table>';

This is a quick and dirty way to output a MySQL table, but if you also want the names of the columns to show up, things get quite a bit trickier. 这是一种输出MySQL表的快速而肮脏的方法,但是如果您还希望显示列的名称,则事情会变得有些棘手。 Also, if your results contain special characters such as ">", this can mess up the output. 另外,如果您的结果包含特殊字符(例如“>”),则可能会使输出混乱。 If that happens, just look up the documentation for htmlspecialchars() . 如果发生这种情况,只需查找htmlspecialchars()的文档。

If you want this table to show up on the same page as your form, just create a submit button and check the value in PHP like so: 如果希望此表与表单显示在同一页面上,只需创建一个提交按钮,然后像这样检查PHP中的值:

if (isset($_POST['SubmitButtonNameHere'])  { ... }

I'm not sure of the structure of your form so this code might need to be different to determine whether the form has been submitted. 我不确定您的表单结构,因此该代码可能需要不同才能确定表单是否已提交。 You can put the code for creating the table inside of the curly braces and place the code for your form either above or below the if statement. 您可以将用于创建表的代码放在大括号内,并将表单的代码放在if语句的上方或下方。

<tr> is an HTML element used to add rows to a <table> element. <tr>是一个HTML元素,用于向<table>元素添加行。 The <td> element adds cells to those rows. <td>元素将单元格添加到这些行。 Also, your code was using mysql_* . 另外,您的代码使用的是mysql_* This was changed to mysqli_* as the mysql prefix is deprecated in PHP. 由于PHP中不推荐使用mysql前缀,因此将其更改为mysqli_* Hope this answers your question. 希望这能回答您的问题。

Just use this code instead :) 只需使用此代码即可:)

$google= mysql_query($query) or die(mysql_error());

echo "<table><tbody>";
while($row = mysql_fetch_assoc($google)){
foreach($row as $cname => $cvalue){
echo "<tr><td>".$cname."</td><td>".$cvalue."</td></tr>";
}
echo "</tbody></table>";

Done :) 完成:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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