在网页上的HTML表格中显示MySQL数据库表中的值我想从数据库表中检索值并在页面的html表中显示它们。我已经搜索了这个,但我找不到答案,虽然这肯定是容易的(这应该是数据库的基础知识)。我想我搜索过的词语具有误导性。数据库表名称是票证,它现在有6个字段(submission_id,formID,IP,名称,电子邮件和消息),但应该有另一个名为ticket_number的字段。如何让它在html表中显示db中的所有值,如下所示:<table border="1">
<tr>
<th>Submission ID</th>
<th>Form ID</th>
<th>IP</th>
<th>Name</th>
<th>E-mail</th>
<th>Message</th>
</tr>
<tr>
<td>123456789</td>
<td>12345</td>
<td>123.555.789</td>
<td>John Johnny</td>
<td>johnny@example.com</td>
<td>This is the message John sent you</td>
</tr></table>然后是“约翰”之下的所有其他值。
3 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
试试这个:(完全动态...)
<?php $host = "localhost";$user = "username_here";$pass = "password_here";$db_name = "database_name_here";//create connection$connection = mysqli_connect($host, $user, $pass, $db_name);//test if connection failedif(mysqli_connect_errno()){ die("connection failed: " . mysqli_connect_error() . " (" . mysqli_connect_errno() . ")");}//get results from database$result = mysqli_query($connection,"SELECT * FROM products");$all_property = array(); //declare an array for saving property//showing propertyecho '<table class="data-table"> <tr class="data-heading">'; //initialize table tagwhile ($property = mysqli_fetch_field($result)) { echo '<td>' . $property->name . '</td>'; //get field name for header array_push($all_property, $property->name); //save those to array}echo '</tr>'; //end tr tag//showing all datawhile ($row = mysqli_fetch_array($result)) { echo "<tr>"; foreach ($all_property as $item) { echo '<td>' . $row[$item] . '</td>'; //get items using property value } echo '</tr>';}echo "</table>";?>
当年话下
TA贡献1890条经验 获得超9个赞
首先,连接到数据库:
$conn=mysql_connect("hostname","username","password");mysql_select_db("databasename",$conn);
您可以使用它来显示单个记录:
例如,如果URL是/index.php?sequence=123
,则下面的代码将从表中选择,其中sequence = 123
。
<?php $sql="SELECT * from table where sequence = '".$_GET["sequence"]."' ";$rs=mysql_query($sql,$conn) or die(mysql_error());$result=mysql_fetch_array($rs);echo '<table> <tr> <td>Forename</td> <td>Surname</td> </tr> <tr> <td>'.$result["forename"].'</td> <td>'.$result["surname"].'</td> </tr> </table>';?>
或者,如果要列出与表中的条件匹配的所有值:
<?php echo '<table> <tr> <td>Forename</td> <td>Surname</td> </tr>';$sql="SELECT * from table where sequence = '".$_GET["sequence"]."' ";$rs=mysql_query($sql,$conn) or die(mysql_error());while($result=mysql_fetch_array($rs)){echo '<tr> <td>'.$result["forename"].'</td> <td>'.$result["surname"].'</td> </tr>';}echo '</table>';?>
添加回答
举报
0/150
提交
取消