首先,我使用 PHP 从 MySQL 数据库中获取值。这是我的 PHP 代码:
<?php
$dsn = 'mysql:host=localhost;dbname=iot';
$username = 'root';
$password = '';
$dbh = new PDO($dsn, $username, $password);
//build the query
$query="SELECT temperaturevalue, humidityvalue FROM sensors";
//execute the query
$data = $dbh->query($query);
//convert result resource to array
$result = $data->fetchAll(PDO::FETCH_ASSOC);
//view the entire array (for testing)
//print_r($result);
//display array elements
foreach($result as $output) {
echo "temperature Value";
echo $output['temperaturevalue'];
echo "<br/>";
echo "<br/>";
echo "humidity ";
echo $output['humidityvalue'] ;
}
?>
现在我想在 HTML 页面上显示这个值。这是我的 html 代码:
<!DOCTYPE html>
<html>
<head>
<title>Smart house</title>
<meta http-equiv="refresh" content="30">
<meta charset="UTF-8">
<style>
h1 {color:orange;
font-style: italic;
font-size:300%}
img {
width:100%;
}
</style>
</head>
<body>
<h1><strong>Connect and control</strong></h2>
<P style="text-align:center;"><img src="myhouse.png" alt="smart house" style="width:50px;height:40px;"></p>
<br>
</body>
</html>
我应该添加什么来显示在我的 html 页面上,例如:
Temperature value : 25.56
Humidity value : 300
最佳答案
在 Html 页面中无法显示 php 数据。
- 将文件类型从“.Html”更改为“.php”
- 将上面的 php 添加到要显示数据的“foreach”部分。还要根据您的设计(HTML)结构调整“echo”部分。
关于php - 将值从 php 传递到 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36396556/