Code sample

This trivial PHP script returns a list of all chords in the DB


Confidential details have been altered
<?
  # connect to db
  include("hidden.inc");
  connect() or die("Connection failed");

  # issue query
  $query = "select root, type, pos, shape, fingering "
         . "from description, chord, fingering "
         . "where description.id = chord.description_id "
         . "and chord.id = fingering.chord_id";
  $result = mysql_query($query) or die("Query failed");
  print("<table border=1>\n");

  # headers
  print ("<tr>\n");
  print("\t<th>root</th>\n");
  print("\t<th>type</th>\n");
  print("\t<th>pos</th>\n");
  print("\t<th>shape</th>\n");
  print("\t<th>fingering</th>\n");
  print("</tr>\n");

  # rows
  while ($row = mysql_fetch_row($result))
  {
    print ("<tr>\n");
    for ($i = 0; $i < mysql_num_fields($result); $i++)
    {
      # escape any special chars and print
      printf("\t<td>%s</td>\n", htmlspecialchars($row[$i]));
    }
    print("<tr>\n");
  }

  print("</table>\n");
  mysql_free_result($result);
?>