我了解HTML的基础知识,而不了解PHP。我找到了大量的教程,但是我找不到一个教程。我有一个mySQL数据库工作和apache运行。如何在页面上显示整个表格?我提前不知道列名。
编辑:添加并调整了代码。我的索引页面显示了这个:connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } //$sql = "SHOW TABLES"; $sql = "select * from cpu"; //edit your table name here $res = $mysqli->query($sql); while ($row = $res->fetch_assoc()) { print_r($row); } ?>
这是我的整个index.html来源:
<?php
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
将文件更改为.php后,错误消息更加清晰。它提醒我,我没有webuser在192.168.1.2。我再次创建了用户并提供了正确的权限。该功能似乎有效,但我会在几个小时内有更多时间来查看它。
答案 0 :(得分:1)
这里真正的问题是您在网页上看到connect_errno) { echo "Failed to...
,而不是仅以Failed to...
开头的行。
因为您的浏览器认为从<?php
到$mysqli->
的代码是HTML代码(由于打开和关闭<>
,我认为您的文件是.htm(l)文件而不是.php文件。
只需更改文件扩展名,如果PHP安装正确,它应该可以正常工作。
答案 1 :(得分:0)
<?php
$mysqli = new mysqli("localhost", "root", "", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from tbl_comment"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
print_r($row);
}
?>
答案 2 :(得分:0)
据我所知。您想要显示值,但不知道列名称。 如果是这样,试试这个。
$mysqli = new mysqli("192.168.1.2", "webuser", "*****", "OCN");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//$sql = "SHOW TABLES";
$sql = "select * from cpu"; //edit your table name here
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
foreach($row as $ind => $val)
{
echo "Column name: $ind, Column Value: $val<br />";
}
echo "<hr />";
}
?>