Firts我使用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
答案 0 :(得分:1)
在Html页面中,您无法显示php数据。
答案 1 :(得分:0)
这很简单。当您需要回显值时,您只需打开应答器。但请确保您的html文件必须是.php扩展名,因为html文件不能包含任何php代码。
<h1><strong>Connect and control</strong></h2>
Temperature value : <?php echo $output['temperaturevalue']; ?>
Humidity value : <?php echo $output['humidityvalue']; ?>
<br>
编辑:要清楚我只谈论一个php页面。
答案 2 :(得分:0)
这样做
制作.php文件并在.php文件中包含.html文件,然后就可以在html文件上打印值
示例:
<强> test.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);
// foreach code removed and added to html file.
include("index.html");
?>
<强>的index.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>
<?php
//display array elements
foreach($result as $output) {
echo "temperature Value";
echo $output['temperaturevalue'];
echo "<br/>";
echo "<br/>";
echo "humidity ";
echo $output['humidityvalue'] ;
}
?>
<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>
希望这会对你有帮助!
答案 3 :(得分:0)
您需要将文件合并到一个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);
?>
<!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>
<?
//display array elements
foreach($result as $output) {
echo "temperature Value";
echo $output['temperaturevalue'];
echo "<br/>";
echo "<br/>";
echo "humidity ";
echo $output['humidityvalue'] ;
}
?>
</body>
</html>
您可以毫无问题地组合HTML和PHP代码。
答案 4 :(得分:0)
如果您在HTML页面上显示,则无法执行此操作。你必须 将文件扩展名HTML更改为PHP .....然后将你的foreach循环改为 此
<h1><strong>Connect and control</strong></h2>
<?php foreach($result as $output) { ?>
Temperature value : <?php echo $output['temperaturevalue']; ?>
Humidity value : <?php echo $output['humidityvalue']; ?>
<?php } ?>
尝试使用这些代码即可。