在一个漂亮的表中使用PHP显示来自MYSQL的数据
我对PHP和MYSQL都是新手,我正试图弄清楚如何在表中很好地显示查询结果。我对PHP和MYSQL以及HTML都是新手 到目前为止,我设法在我的机器上创建了一个数据库,创建了一些表,在表中插入了一些数据,并创建了一个PHP页面,我可以用它来发出查询。我成功地检索了所需的查询,但我不知道如何使其在表中看起来美观,并显示列名。此外,我也不知道如何处理随机表信息,因为我的代码是针对特定表硬编码的。这是我到目前为止的PHP代码在一个漂亮的表中使用PHP显示来自MYSQL的数据,php,mysql,Php,Mysql,我对PHP和MYSQL都是新手,我正试图弄清楚如何在表中很好地显示查询结果。我对PHP和MYSQL以及HTML都是新手 到目前为止,我设法在我的机器上创建了一个数据库,创建了一些表,在表中插入了一些数据,并创建了一个PHP页面,我可以用它来发出查询。我成功地检索了所需的查询,但我不知道如何使其在表中看起来美观,并显示列名。此外,我也不知道如何处理随机表信息,因为我的代码是针对特定表硬编码的。这是我到目前为止的PHP代码 <html> <head><title>
<html>
<head><title>PHP MYSQL</title></head>
<body>
<form action="." method="GET">
<textarea name="query" cols="60" rows="8"></textarea><br />
<input type="submit" value="Submit" />
</form>
<?php
DEFINE('DB_USER', 'cs143');
DEFINE('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'TEST');
$db_connection = mysql_connect("localhost", "cs143", "")
OR die("could not connect to databaseaaa !!!");
mysql_select_db("TEST", $db_connection);
$query = $_GET[query];
$rs = mysql_query($query, $db_connection);
if(!$rs)
{
die("Query failed");
}
while($row = mysql_fetch_row($rs))
{
$name = $row[0];
$age = $row[1];
print "$name, $age<br />";
}
?>
</body>
</html>
PHP-MYSQL
名称
年龄
我想我找到了我要找的东西。我使用以下代码,它获取列数及其名称并正确显示数据
<h2>Query result:</h2>
<html><body><table border=1 cellspacing=1 cellpadding=2><tr align="center">
<?php
//print out first row of header fields
$i = 0;
while($i < mysql_num_fields($rs))
{
//meta holds column information (name, description, etc.)
$meta = mysql_fetch_field($rs, $i);
//output column names along with bold HTML tags via echo
echo '<td><b>' . $meta->name . '</b></td>';
$i = $i + 1;
}
?>
<?php
//print out the actual query results
$i = 0;
while($row = mysql_fetch_row($rs))
{
echo '<tr align="center">';
$count = count($row);
$y = 0;
while($y < $count)
{
$c_row = current($row);
//if any value is NULL (blank), replace it with N/A
if($c_row==NULL)
echo '<td>N/A</td>';
else
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
?>
查询结果:
Cool,因此我可以使用HTML表格显示数据。但是,如果我的表有任意数量的列(例如),如何显示它呢?我需要mysql_num_fields函数吗?当然,您可以将html与php结合使用。。如果有另一列,则使用相同的函数,然后创建一个新的
和echo$row[n]
。我的问题是此代码仍然是硬编码的。我有很多表,我需要用户能够将所需的查询输入到textarea,并从具有任意列数的任意表返回结果。好吧,我们对此无能为力。。。我们不能让值在php中神奇地出现D
<h2>Query result:</h2>
<html><body><table border=1 cellspacing=1 cellpadding=2><tr align="center">
<?php
//print out first row of header fields
$i = 0;
while($i < mysql_num_fields($rs))
{
//meta holds column information (name, description, etc.)
$meta = mysql_fetch_field($rs, $i);
//output column names along with bold HTML tags via echo
echo '<td><b>' . $meta->name . '</b></td>';
$i = $i + 1;
}
?>
<?php
//print out the actual query results
$i = 0;
while($row = mysql_fetch_row($rs))
{
echo '<tr align="center">';
$count = count($row);
$y = 0;
while($y < $count)
{
$c_row = current($row);
//if any value is NULL (blank), replace it with N/A
if($c_row==NULL)
echo '<td>N/A</td>';
else
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '</tr>';
$i = $i + 1;
}
?>