Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Spring 2020

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

    #!/usr/bin/php
    <?php
    // Connecting, selecting database
      $mysqli = new mysqli('localhost','class','cscie12','coursecatalog');
      if ($mysqli->connect_errno) {
          echo "Failed to connect to mysql: ".$mysqli->connect_errno." ".$mysqli->connect_error;
      }

    // Performing SQL query
    $query = 'SELECT distinct department_short, department_code FROM courses order by department_short';
    $result = $mysqli->query($query);

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

### Output

[dheitmey@apophis courses]$ php dept-txt.php AAAS African and African American Studies AMER American Studies ANTH Anthropology URBP Architecture, Landscape Architecture, and Urban Planning ABEA Asian Studies Programs ASTR Astronomy BSDM Biological Sciences in Dental Medicine BSPH Biological Sciences in Public Health BIPH Biophysics BIST Biostatistics CELT Celtic Languages and Literatures CHPB Chemical and Physical Biology CHBI Chemical Biology CHEM Chemistry and Chemical Biology CPLT Comparative Literature DRAM Dramatic Arts E&PS Earth and Planetary Sciences EALC East Asian Languages and Civilizations ECON Economics DEAS Engineering and Applied Sciences ENGH English ESPP Environmental Science and Public Policy EXPO Expository Writing FOLK Folklore and Mythology FRSP Freshman Seminars GEN ED General Education GERM Germanic Languages and Literatures GLOBHLTH Global Health and Health Policy GOVM Government HPOL Health Policy HIST History HLIT History and Literature HAA History of Art and Architecture HSCI History of Science HSEM House Seminars HEB Human Evolutionary Biology HUM Humanities LSCI Life Sciences LING Linguistics MATH Mathematics MDSC Medical Sciences MDST Medieval Studies MEST Middle Eastern Studies MBB Mind, Brain, and Behavior MCB Molecular and Cellular Biology MUSC Music NELC Near Eastern Languages and Civilizations NEURO Neurobiology NODEPT No Department BIOE Organismic and Evolutionary Biology PHIL Philosophy PSCI Physical Sciences PHYS Physics PSYC Psychology RSEA Regional Studies - East Asia ROML Romance Languages and Literatures REECA Russia, Eastern Europe, and Central Asia SLAV Slavic Languages and Literatures SPOL Social Policy SOST Social Studies SOCL Sociology SAST South Asian Studies SPCN Special Concentrations STAT Statistics SCRB Stem Cell and Regenerative Biology WMGS Studies of Women, Gender, and Sexuality SBIO Systems Biology CLAS The Classics RELG The Study of Religion UKRA Ukrainian Studies VES Visual and Environmental Studies

Copyright © David Heitmeyer