PHP pages with MariaDB (extra)
The labs show the data with Flask, but the Pi already has PHP from LAMP, and it can do the same job with no program to keep running: Apache runs the PHP code every time someone opens the page. This is beyond the lab sheets, for anyone who wants to try.
Where PHP files go
Files in /var/www/html are served by Apache. /var/www/html/sensor.php is http://localhost/sensor.php. The file must end in .php to be run as PHP.
The folder belongs to root. Either use sudo nano, or give yourself access once (how).
Check PHP works
echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php
Open http://localhost/info.php. Search the page for mysqli: if it is listed, PHP can talk to MariaDB. Delete the file afterwards (it shows a lot about your system): sudo rm /var/www/html/info.php.
PHP in five lines
<?php
$temp = 25.3; // variables start with $
echo "<p>Temperature: " . $temp . "</p>"; // . joins text
if ($temp > 30) { echo "<p>Hot!</p>"; }
?>
Code goes between <?php and ?>; everything outside is plain HTML. Every statement ends with ;.
Show the sensor table (sensor.php)
<!DOCTYPE html>
<html>
<head>
<title>Sensor Data</title>
<meta http-equiv="refresh" content="5">
<style> table, th, td { border: 1px solid; padding: 4px; } </style>
</head>
<body>
<h1>Latest sensor readings</h1>
<?php
$db = new mysqli("localhost", "Nypuser", "password", "MySensor");
if ($db->connect_error) {
die("<p>Database connection failed: " . htmlspecialchars($db->connect_error) . "</p>");
}
$result = $db->query("SELECT SerialNo, Temp, Dist, SensorDate FROM Data ORDER BY SerialNo DESC LIMIT 20");
echo "<table><tr><th>No.</th><th>Temp</th><th>Dist</th><th>Time</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>";
foreach ($row as $value) { echo "<td>" . htmlspecialchars($value) . "</td>"; }
echo "</tr>";
}
echo "</table>";
$db->close();
?>
</body>
</html>
fetch_assoc() gives each row with column names ($row["Temp"]), which is easier to read than numbers. htmlspecialchars stops stored text from being run as HTML.
Seeing PHP errors
A blank page or 500 error means PHP stopped with an error. The error is in Apache's log:
sudo tail -n 20 /var/log/apache2/error.log
While developing you can show errors on the page instead, by adding this as the first line of the file:
<?php ini_set('display_errors', 1); error_reporting(E_ALL); ?>
Remove it when finished.
Common problems
| Problem | Fix |
|---|---|
| The browser shows the PHP code, or downloads the file | PHP is not active in Apache: sudo apt install libapache2-mod-php, sudo systemctl restart apache2. Opened as file:///...? Use http://localhost/.... |
Class "mysqli" not found | sudo apt install php-mysql, then restart Apache. |
Access denied for user 'Nypuser'@'localhost' | Test mysql -u Nypuser -p in the terminal; fix the user (MariaDB users). |
Unknown database 'mysensor' | Case-sensitive: MySensor. |
No such file or directory (connection) | MariaDB is not running: sudo systemctl start mariadb. |
| Blank page / 500 | Read /var/log/apache2/error.log. |
| 403 Forbidden | File not readable by Apache: sudo chmod 644 /var/www/html/sensor.php. |
| 404 Not Found | Not in /var/www/html, or the name's capitals differ. |