Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. <?php
  2.  
  3. function Connection(){
  4. $server="localhost";
  5. $user="root";
  6. $pass="123456";
  7. $db="database";
  8.  
  9. $connection = mysqli_connect($server, $user, $pass);
  10.  
  11. if (!$connection) {
  12. die('MySQL ERROR: ' . mysqli_error());
  13. }
  14.  
  15. mysqli_select_db($connection,$db ) or die( 'MySQL ERROR: '. mysqli_error() );
  16.  
  17. return $connection;
  18. }?>
  19.  
  20. <?php
  21. include("connect.php");
  22.  
  23. $link=Connection();
  24.  
  25. $temp1=$_POST['temp1'];
  26. $hum1=$_POST['hum1'];
  27.  
  28. $query = "INSERT INTO `templog` (`temperature`, `humidity`)
  29. VALUES ('".$temp1."','".$hum1."')";
  30.  
  31. mysqli_query($link,$query);
  32. mysqli_close($link);
  33. header("Location: index.php");?>
  34.  
  35. <?php
  36.  
  37. include("connect.php");
  38.  
  39. $link=Connection();
  40.  
  41. $result=mysqli_query($link,"SELECT * FROM `templog` ORDER BY `timeStamp` DESC");
  42. ?>
  43.  
  44. <html>
  45. <head>
  46. <title>Sensor Data</title>
  47. </head>
  48. <body>
  49. <h1>Temperature / moisture sensor readings</h1>
  50.  
  51. <table border="1" cellspacing="1" cellpadding="1">
  52. <tr>
  53. <td>&nbsp;Timestamp&nbsp;</td>
  54. <td>&nbsp;Temperature&nbsp;</td>
  55. <td>&nbsp;Moisture&nbsp;</td>
  56. </tr>
  57.  
  58. <?php
  59. if($result!==FALSE){
  60. while($row = mysqli_fetch_array($result)) {
  61. printf("<tr><td> &nbsp;%s </td><td> &nbsp;%s&nbsp; </td><td> &nbsp;%s&nbsp; </td></tr>",
  62. $row["timeStamp"], $row["temperature"], $row["humidity"]);
  63. }
  64. mysqli_free_result($result);
  65. mysqli_close($link);
  66. }
  67. ?>
  68.  
  69. </table>
  70. </body>
  71. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement