<html>
<head>
<title>Test PHP Database Connection to Mysql</title>
</head>
<body>
<h1>Test PHP Database Connection to Mysql</h1>
<br><br>
<?php
// $host = 'workshop.sps.nyu.edu:3306';
$host = 'localhost';
$db = 'demo';
$user = 'demo';
$pswd = 'demo';
// Connect to MySql database server -------------------------------------
$connect = mysqli_connect($host,$user,$pswd,$db);
if (! $connect) {
die('Could not connect: ' . mysqli_connect_error());
}
print("<b>Connected to DB server on: $host</b><br>");
// perform select query -------------------------------------------------
$result = mysqli_query($connect, 'select fname, lname, ssn from student');
if (! $result) {
die('Could not execute query: ' . mysqli_error($connect));
}
while ($row = mysqli_fetch_assoc($result)) {
echo $row['fname'],' ',$row['lname'],' ',$row['ssn'],'<br>';
}
// release resources ----------------------------------------------------
mysqli_free_result($result);
mysqli_close($connect);
?>
</body>
</html>