Skip to content

Instantly share code, notes, and snippets.

@TrickSumo
Created September 5, 2020 12:04
Show Gist options
  • Save TrickSumo/40446142d06283db73875a92b5ac67c0 to your computer and use it in GitHub Desktop.
Save TrickSumo/40446142d06283db73875a92b5ac67c0 to your computer and use it in GitHub Desktop.
PHP file to read data from database for nodemcu
// Code Written by Rishi Tiwari
// Website:- https://tricksumo.com
// Reference:- https://www.w3schools.com/php/php_mysql_select.asp
//
//
<?php
$host = "localhost"; // host = localhost because database hosted on the same server where PHP files are hosted
$dbname = "........."; // Database name
$username = "........."; // Database username
$password = "........."; // Database password
// Establish connection to MySQL database
$conn = new mysqli($host, $username, $password, $dbname);
// Check if connection established successfully
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else { echo "Connected to mysql database. "; }
// If values send by NodeMCU are not empty then insert into MySQL database table
if(!empty($_POST['sendval']))
{
$val = $_POST['sendval'];
$sql = "SELECT id, val, val2, date, time FROM tricksumo_nodemcu where id = $val"; // Update your tablename here
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id:" . $row["id"]. " ,val:" . $row["val"]. " ,val2:" . $row["val2"]. " ,date:" . $row["date"]." ,time:" .$row["time"]. "";
}
} else {
echo "0 results";
}
}
// Close MySQL connection
$conn->close();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment