PHP和mysqli:从数据库的HTML表 [英] PHP and mysqli: HTML table from database

查看:119
本文介绍了PHP和mysqli:从数据库的HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从数据库中提取几个字段的数据,并使用mysqli填充HTML表。我试图研究这个问题已经提出了解决方案,使用mysql或似乎混合mysql与mysqli。我一直在尝试使用API​​来找到可能工作的函数,但似乎不能获得显示表。任何建议,将不胜感激。

I'm trying to pull the data for a couple of fields out of a database and populate an HTML table using mysqli. My attempts to research the problem have come up with solutions that either use mysql or seem to mix mysql with mysqli. I've been attempting to use the API to find functions that might work, but can't seem to get the table to display. Any suggestions would be appreciated.

<?php
ini_set('display_errors', 'On');
ini_set('display_startup_errors', 'On');
error_reporting(E_ALL);

$mysqli = new mysqli("$dbhost", "$dbuser", "$dbpass", "$dbname");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

if (!$stmt = $mysqli->query("SELECT aid, aname FROM actor_info")) {
    echo "Query Failed!: (" . $mysqli->errno . ") ". $mysqli->error;
}

if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}

if (!$stmt->bind_result($aid, $aname)) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}

echo "ACTOR INFORMATION";
echo "<table border='1' cellpadding='2' cellspacing='2'";
echo "<tr><td>ID</td><td>NAME</td>";
while ($row = mysqli_fetch_array($stmt)) {
        echo "<tr>";
        echo "<td>".$aid."</td>";
        echo "<td>".$aname."</td>";
        echo "</tr>";
}
else {
    echo "No records found";
}
$stmt->free();
$mysqli->close();
?>


推荐答案

while ($row = mysqli_fetch_array($stmt)) {
    echo "<tr>";
    echo "<td>" . $row["aid"] . "</td>";
    echo "<td>" . $row["aname"] . "</td>";
    echo "</tr>";
}

这篇关于PHP和mysqli:从数据库的HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆