## PHP Script to Connect to Database and Get List of Departments

    <?php

    // set header for text content
    header('Content-type: text/plain');

    // use Dotenv to load database info from .env file
    // DB_HOST, DB_USER, DB_PW, DB_NAME
    require_once('vendor/autoload.php');
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->safeLoad();

    // Connecting, selecting database
    $mysqli = new mysqli($_ENV['DB_HOST'],$_ENV['DB_USER'],$_ENV['DB_PW'],$_ENV['DB_NAME']);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to mysql: ".$mysqli->connect_errno." ".$mysqli->connect_error;
    }

    // Performing SQL query
    $query = 'SELECT DISTINCT department, department_code FROM ext_courses ORDER BY department';
    $result = $mysqli->query($query);

    // iterating through results
    while ($row = $result->fetch_assoc()){
      echo $row['department_code'];
      echo "\t";
      echo $row['department'];
      echo "\n";
    }
    ?>

### Output

[dph445@cs12:~/www-cscie12/html/cgi/ext-courses$ php dept-txt.php AAAS African and African American Studies ANTH Anthropology and Archaeology APMA Applied Mathematics ARAB Arabic ASTR Astronomy BIOS Biological Sciences BIOT Biotechnology CELT Celtic Languages and Literatures CHEM Chemistry CLAS Classics CSCI Computer Science CREA Creative Writing DEVP Development Practice DGMD Digital Media DRAM Dramatic Arts ECON Economics EDUC Education ENSC Engineering Sciences ENGL English ENVR Environmental Studies EXPO Expository Writing FREN French Language and Literature GERM German GOVT Government CGRK Greek HIST History HARC History of Art and Architecture HUMA Humanities IORP Industrial-Organizational Psychology ISMT Information Systems Management ITAL Italian JAPA Japanese JOUR Journalism LATI Latin LSTU Legal Studies MGMT Management MATH Mathematics MUSE Museum Studies MUSI Music NUTR Nutrition PHIL Philosophy PHYS Physics PSYC Psychology RELI Religion SSCI Social Sciences SOCI Sociology SPAN Spanish Language and Literature SPCH Speech STAT Statistics STAR Studio Arts and Film