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