Web Programming Languages

PHP Source Code: dbread.php

<?php
//----------------------------------------------------------
//   The Web Language Project
//   Mark Brautigam
//   May-June 2015
//   http://www.mixed-up.com/markb/
//----------------------------------------------------------

include "common.php";
headers1();
headers2();
echo sidebar("php", "dbread", "php");
?>
  
  <div id="content">
    <h2>PHP: Display MySQL Database Contents</h2>
    <table class='results db'>
      <tr><th>Park</th><th>City</th><th>State</th><th>Geo</th><th>Edit</th></tr>

<?php
  // Fetch each database entry and write it into a row
  // in an HTML table
  //
  include "./connect.php";
  $dbh = my_connect ();
  if (function_exists ('mysqli_query')) {
    $qresult = $dbh->query ("SELECT * FROM parks");
    while ($row = $qresult->fetch_assoc()) {
      write_row_data ($row);
    }
  }
  else {
    $qresult = mysql_query ("SELECT * FROM parks");
    while ($row = mysql_fetch_assoc($qresult)) {
      write_row_data ($row);
    }
  }
?>

    </table>
    <p><a href='dbwrite.php'>Write to this database table »</a></p>
    <p><a href='source.php?f=7'>Show PHP source code »</a>
  </div>

<?php 
footers(); 

  //-------------------------------------------------------------
  function write_row_data ($row)
  //-------------------------------------------------------------
  {
    echo "<tr><td>{$row['site']}</td><td>{$row['city']}</td><td>{$row['state']}</td>";
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $lat2 = number_format ($lat, 2);
    $lon2 = number_format ($lon, 2);
    printf ("<td><a target='_blank' " . 
      "href='https://www.google.com/maps/preview/@%s,%s,12z'>%s, %s</a></td>\n",
      $lat, $lon, $lat2, $lon2);
    echo "<td><a href='dbedit.php?id={$row['ID']}'><img src='../images/pencilc.svg' /></a></td>\n";
    echo "</tr>\n";
  }
?>