wiki:Docs/825gen2/Dev/Networking/PHP

PHP

The 825gen2 includes PHP https://en.wikipedia.org/wiki/PHP which combined with the Apache Web Server allows for powerful web features.

From a terminal php -v will show the PHP version.

card825gen2:~$ php -v
PHP 8.1.10 (cli) (built: Aug 30 2022 16:09:36) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.10, Copyright (c) Zend Technologies

Accessing MySQL database from php:

Make sure MySQL is configured first. Refer to the MySQL page MySQL (MariaDB)

Create a simple test php file to read from the database

cd /usr/share/apache2/default-site/htdocs
nano readmysql.php
<!DOCTYPE html>
<html lang="en">
<head>
  <title>825 MySQL Test</title>
</head>
<body>

<?php
$conn = mysqli_connect('127.0.0.1', 'dbuser', '81440', 'test');
if(!$conn) {
   echo 'Connection error: ' . mysqli_connect_error();
} else {
   $sql = 'select * from test_tbl;';
   $result = $conn->query($sql);
   if($result->num_rows > 0) {
      echo '<table><tr><th>Title</th><th>Description</th></tr>';
      while($row = $result->fetch_assoc()) {
          $title = $row['title'];
          $description = $row['description'];
          echo '<tr><td>' . $title . '</td><td>' . $description . '</td></tr>';
      }
      echo '</table>';
   }
}
?>

</body>
</html>

Test with PC on same network as 825.

It may also be helpful to use browser "Developer tools" selection to view the generated html code.

PHP can also be used to read SQLite databases.

card825gen2:/usr/share/apache2/default-site/htdocs$ cat testsqlite.php
<!DOCTYPE html>
<html lang="en">
<head>
  <title>825 SQLite Test</title>
</head>
<body>

<?php
$db = new SQLite3('/mnt/nand/apps/ids/ids.db3');

$results = $db->query('SELECT truck_name,tare FROM trucks');
echo '<table><tr><th>Truck ID</th><th>Tare</th></tr>';
while ($row = $results->fetchArray()) {
   $truckid = $row['truck_name'];
   $tare = $row['tare'];
   echo '<tr><td>' . $truckid . '</td><td>' . $tare . '</td></tr>';
}
echo '</table>';
?>

</body>
</html>

Following is a more advanced demo. This reads from an ID storage MySQL transaction table and also displays vehicle images associated with transactions.

ids.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>825 ID Storage</title>
  <style type="text/css" media="screen">
  table tr td {
    text-align: right;
    padding: 0 15px;
  }
  table td:nth-child(3) { text-align: left; }
</style>
</head>
<body>
<?php
$conn = mysqli_connect('127.0.0.1', 'dbuser', '81440', 'test');
if(!$conn) {
  echo 'Connection error: ' . mysqli_connect_error();
} else {
  $sql = 'select * from trans;';
  $result = $conn->query($sql);
  if($result->num_rows > 0) {
    echo '<table><tr><th>Tran Num</th><th>date/time</th><th>Vehicle<br/>Product<br/>Customer</th><th>Gross Wt</th><th>Tare Wt</th><th>Net Wt</th><th>Image</th><th>First pass</th></tr>';
    while($row = $result->fetch_assoc()) {
      $tkt_num = $row['tkt_num'];
      $datetime = $row['date'] . ' ' . $row['time'];
	  $veh = $row['truck'];
	  $prod = $row['prod'];
	  $cust = $row['cust'];
	  $job = $row['job'];
      $gross = $row['gross'];
      $tare = $row['tare'];
      $net = $row['net'];
      echo '<tr><td>' . $tkt_num . '</td><td>' . $datetime . '</td><td>' . $veh . '<br/>' . $prod . '<br/>' . $cust . '</td><td>' . $gross . '</td><td>' . $tare . '</td><td>'. $net  . '</td>';
      $imgfile = sprintf("v%06d_1.jpg", $tkt_num);
      $img = 'images/' . $imgfile;
      if(file_exists($img)) {
        echo '<td><a href="' . $img . '"><img src="' . $img .'" width="128" height="96" ></a></td>';
      } else {
        echo '<td></td>';
      }
      $imgfile = sprintf("v%06da_1.jpg", $tkt_num);
      $img = 'images/' . $imgfile;
      if(file_exists($img)) {
        echo '<td><a href="' . $img . '"><img src="' . $img .'" width="128" height="96" ></a></td>';
      } else {
        echo '<td></td>';
      }
	  echo '</tr>';
    }
    echo '</table>';
  }
}
?>
</body>
</html>

Last modified 2 months ago Last modified on 02/29/24 11:47:19

Attachments (4)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.