将值从php传递给html

Firts I took values from MySQL database using PHP . This is my PHP code :

<?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'] ;

}

?>

Now I want to display this Value on an HTML page. This my html code :

<!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>

What should I add to display on my html page for exemple :

Temperature value : 25.56
Humidity value : 300

This is very simple. You just have to open balises when you need to echo the values. but be certain that your html file must be .php extension because html files can't contain any php codes.

<h1><strong>Connect and control</strong></h2>
Temperature value : <?php echo $output['temperaturevalue']; ?>
Humidity value : <?php echo $output['humidityvalue']; ?>
<br>

Edit : To be clear i'm talking about one php page only.

In Html page you cannot show php data.

  1. Change file type from ".Html" to ".php"
  2. Add your above php the "foreach" part where you want to show the data. Also adjust the "echo" part as per your design(HTML) structure.

Do like this

Make .php file and include .html file in .php file then you can print value on html file

Example :

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>

Hope this will help you!

You need to merge your file in one PHP file:

<?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>

You can combine HTML and PHP code without problem.

If you are displaying on HTML page you can't do that. You have to change file extension HTML to PHP .....then put your foreach loop like this

<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 }  ?>

Try these code it will be worked.