Created
September 5, 2020 12:04
-
-
Save TrickSumo/40446142d06283db73875a92b5ac67c0 to your computer and use it in GitHub Desktop.
PHP file to read data from database for nodemcu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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