我在与以下字段的SQL数据库中的表:ID,名称,电子邮件,大学,语言和经验。我想创建一个从SQL获取数据和最近10个结果输出HTML表?我会怎么做呢?
我很抱歉,如果这是一个很简单的问题,我有PHP和SQL知之甚少。
这是我现在所拥有的,只是显示的名称,而不是在一个表中的代码:
<html>
<head>
<title>Last 5 Results</title>
</head>
<body>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo");
while($row = mysql_fetch_array($results)) {
echo $row['Name'] . "</br>";
?>
</body>
</html>
这是应该帮助你创建表,并得到php和mysql的更多的知识。
另外,应您的连接逻辑和查询移动到过程的开始,从而避免错误,同时加载页面,并显示不仅仅是mysql_error更精确的错误。
编辑:如果你的IDS被递增,那么你可以添加ORDER BY子句,
变化:SELECT * FROM demo LIMIT 10
到:SELECT * FROM demo LIMIT 10 ORDER BY id
<html>
<head>
<title>Last 10 Results</title>
</head>
<body>
<table>
<thead>
<tr>
<td>Id</td>
<td>Name</td>
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("apploymentdevs");
$results = mysql_query("SELECT * FROM demo LIMIT 10");
while($row = mysql_fetch_array($results)) {
?>
<tr>
<td><?php echo $row['Id']?></td>
<td><?php echo $row['Name']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
需要你在数据库中JSON的数据进行编码的选项:http://www.jeasyui.com/documentation/datagrid.php
但是,这看起来很多更有前途:http://phpgrid.com/
在查询结果中调用table( $result );
将生成一个基于HTML的表不管SQL数组,你给它的大小。我希望这有帮助 :)
<?php
function table( $result ) {
$result->fetch_array( MYSQLI_ASSOC );
echo '<table>';
tableHead( $result );
tableBody( $result );
echo '</table>';
}
function tableHead( $result ) {
echo '<thead>';
foreach ( $result as $x ) {
echo '<tr>';
foreach ( $x as $k => $y ) {
echo '<th>' . ucfirst( $k ) . '</th>';
}
echo '</tr>';
break;
}
echo '</thead>';
}
function tableBody( $result ) {
echo '<tbody>';
foreach ( $result as $x ) {
echo '<tr>';
foreach ( $x as $y ) {
echo '<td>' . $y . '</td>';
}
echo '</tr>';
}
echo '</tbody>';
}
我很恼火粘贴在一起,相同的代码一遍又一遍地从SQL查询建立HTML表格。
所以,在这里我们去与需要mysqli
结果,并共同它们粘贴到,根据那些在数据库中有列名的朴素简单的HTML表格的函数:
function sql_to_html_table($sqlresult, $delim="\n") {
// starting table
$htmltable = "<table>" . $delim ;
$counter = 0 ;
// putting in lines
while( $row = $sqlresult->fetch_assoc() ){
if ( $counter===0 ) {
// table header
$htmltable .= "<tr>" . $delim;
foreach ($row as $key => $value ) {
$htmltable .= "<th>" . $key . "</th>" . $delim ;
}
$htmltable .= "</tr>" . $delim ;
$counter = 22;
}
// table body
$htmltable .= "<tr>" . $delim ;
foreach ($row as $key => $value ) {
$htmltable .= "<td>" . $value . "</td>" . $delim ;
}
$htmltable .= "</tr>" . $delim ;
}
// closing table
$htmltable .= "</table>" . $delim ;
// return
return( $htmltable ) ;
}
实例应用:
$DB = new mysqli("host", "username", "password", "database");
$sqlresult = $DB->query( "SELECT * FROM testtable LIMIT 1 ;" ) ;
echo sql_to_html_table( $sqlresult, $delim="\n" ) ;
你可能有兴趣在获取mysql_fetch_assoc()
(让你在一个关联数组中的数据:键=>值)。在键列名;因此,对于每一行,您可以通过每列(含array_keys()
)循环并打印其值。
$results = mysql_query("SELECT * FROM demo");
while($row = mysql_fetch_assoc($results)) {
foreach (array_keys($row) as $column) {
echo $row[$key] . "</br>";
}
}
(在此之后,你可以缓存在其中只设置一次,因为当你通过运行结果它的值不会改变一个变量array_keys($行)。)
即使它已经有一段时间,我打算把我的2美分:使用的功能(甚至更好 - 类)。创建从MySQL结果表是一种很常见的任务。
<!DOCTYPE html>
<html>
<head><title>Create Tables from MySQL using functions</title></head>
<body>
<?php
db_connect();
// assuming you have an auto increment id as the first column
$result = mysql_query("SELECT * FROM demo ORDER BY 1 DESC LIMIT 10");
print createTable(array_result($result));
?>
</body>
</html>
<?php
/**
* Quick mysql result function
*
* @param $result
* @return array
*/
function array_result($result)
{
$args = array();
while ($row = mysql_fetch_assoc($result)) {
$args[] = $row;
}
return $args;
}
/**
* Connect to db
*
* @param string $db_host
* @param string $db
*/
function db_connect($db_host = "localhost", $db = "apploymentdevs")
{
$connect = mysql_connect($db_host,"root", "root");
if (!$connect) {
die(mysql_error());
}
mysql_select_db($db);
}
/**
* Create a table from a result set
*
* @param array $results
* @return string
*/
function createTable(array $results = array())
{
if (empty($results)) {
return '<table><tr><td>Empty Result Set</td></tr></table>';
}
// dynamically create the header information from the keys
// of the result array from mysql
$table = '<table>';
$keys = array_keys(reset($results));
$table.='<thead><tr>';
foreach ($keys as $key) {
$table.='<th>'.$key.'</th>';
}
$table.='</tr></thead>';
// populate the main table body
$table.='<tbody>';
foreach ($results as $result) {
$table.='<tr>';
foreach ($result as $val) {
$table.='<td>'.$val.'</td>';
}
$table.='</tr>';
}
$table.='</tbody></table>';
return $table;
}
我希望你知道如何在HTML表,带有<table>, <tr> and <td>.
修正了一个表,你的while循环,并使用"SELECT * FROM demo LIMIT 10"
作为SQL查询。