Fetching a MySQL database with PHP
Displaying the results of the database is kind of similar to record data within the database:
<?php
$servername = "localhost";
$username = "ronan";
$password = "rootroot";
$dbname = "motion";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, alerttime FROM alert";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Alert time: " . $row["alerttime"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
$servername = "localhost";
$username = "ronan";
$password = "rootroot";
$dbname = "motion";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, alerttime FROM alert";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Alert time: " . $row["alerttime"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
This is a thing, but as we need those data to be viewable within a mobile app, we will have to integrate those PHP results within a web page, for this we will use JavaScript with a script similar to:
<script>
const req = new XMLHttpRequest();
req.open('GET', 'http://192.168.1.84/motion/results.php', false);
req.send(null);
document.write(req.responseText);
</script>
Last modified: Thursday, 6 February 2020, 9:20 PM