dongtongjian3127 2016-04-04 07:13
浏览 42

将值从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
  • 写回答

5条回答 默认 最新

  • dtamho6404 2016-04-04 07:19
    关注

    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.

    评论

报告相同问题?