duancaiyi7567 2015-04-04 06:54
浏览 39
已采纳

如何在HTML中使用PHP从SQL表中提取数据

I am building a website for myself (learning). I am trying to use .html for the base:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>JPeG & Chaos LLP - Repair Status</title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
<div class="menubar">
    <ul>
        <li><a href="index.html">Home</a></li>
        <li><a href="services.html">Services</a></li>
        <li><a href="status.html">Repair Status</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="contact.html">Contact Us</a></li>
    </ul>
</div>
<header>JPeG & Chaos LLP</header>

//XYZ

<p>
    blah blah blah blah
</p>
</body>
</html>

Where I put the XYZ, I am wanting to pull a table from mysql and place it in to a table that updates every so many minutes (not concerned about the auto-refresh for now).

I tried adding the following PHP in to my HTML, but it doesn't act like PHP, it just prints the sql scripts :

<?php
$servername = "localhost";
$username = "name";
$password = "pass";
$dbname = "RepairStatus";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, status, tip, eta FROM WorkOrders";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr><th>ID</th><th>Status</th><th>Time In Progress</th><th>Est Time of Completion</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["id"]."</td><td>".$row["status"]." ".$row["tip"]."</td><td>".$row["eta"]."</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

the website shows:

num_rows > 0) { echo ""; // output data of each row while($row = $result->fetch_assoc()) { echo ""; } echo "

and continues through the remainder of the php section of the code.

I did run the full php code on a .php file through the website and it pulls the data just fine :

http://jpegchaos.com/rstatus.php

Any suggestions on how to do this so I can use my CSS / HTML to format the results?

  • 写回答

1条回答 默认 最新

  • doucang2871 2015-04-04 07:43
    关注

    If you want to run php within an .html extension file, create .htaccess in your webroot with this line:

        AddType application/x-httpd-php .html
    

    which instructs apache to process html file as a php file.

    Also, try replacing these two lines, for shorter code

        $result = $conn->query($sql);
    
        if ($result->num_rows > 0) {
    

    with this one:

        if ($result = $conn->query($sql)) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建