以下是我写的代码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>局部刷新</title> </head> <body> <?php header("Content-Type:text/html; charset=utf-8"); $dbms = "mysql"; $dbName ="wsd"; $user = "root"; $pwd = ""; $host = "localhost"; $dsn = "$dbms:host=$host;dbname=$dbName"; try{ $pdo=new PDO($dsn,$user,$pwd); $query="select * from node1 order by rand() limit 1"; $res=$pdo->prepare($query); $res->execute(); ?> <table border="1" width="500"> <tr> <td height="22" align="center" valign="middle">温度</td> <td height="22" align="center" valign="middle">湿度</td> </tr> <?php $result=$res->fetchAll(PDO::FETCH_ASSOC) ; for ($i=0;$i<count($result);$i++){ ?> <tr> <td height="22" align="center" valign="middle"><?php echo $result[$i]['Temperature'];?></td> <td height="22" align="center" valign="middle"><?php echo $result[$i]['Humidity'];?></td> </tr> <?php } } catch(Exception $e){ die("Error!:".$e->getMessage().'<br>'); } ?> </table> </body> </html>