diff --git a/chapter11/project1/databaseConnect.inc.php b/chapter11/project1/databaseConnect.inc.php index 4e8e40e546fb05437c01a35379c64d56b191e3dc..b3dda3ee7be358d15cde1ed2c3c7cda3a35e4d07 100644 --- a/chapter11/project1/databaseConnect.inc.php +++ b/chapter11/project1/databaseConnect.inc.php @@ -2,28 +2,34 @@ <?php # Generic php file mean to connect to an aribtrary database. - function ConnectViaMySQLi($host, $user, $password, $database){ - $connection = mysqli_connect($host, $user, $password, $database); + function ConnectViaMySQLi($host, $user, $password){ - $error = mysql_error(); - if ($error != null) { - $output = "<p>Unable to connect to database.</p>" . $error; - exit($output); + # Create Connection. + $connection = mysqli_connect($host, $user, $password); + + # Check for valid Connection. + if ($connection->connect_error) { + die("Connection Failed: " . $connection->connect_error); } + #echo "Connected Successfully!"; } - function ConnectViaPDO($host, $user, $password, $database) { + function ConnectViaPDO($host, $user, $password, $database, $DBport) { try { - $connectionString = "mysql:host=$host:dbname=$database"; - #echo '$connectionString = '; - #echo $connectionString; + # Create Connection. + $connectionString = "mysql:host=$host;port=$DBport;dbname=$database"; $pdo = new PDO($connectionString, $user, $password); - echo "Connected Successfully!"; + # Not sure what this does, but the w3schools example had it. + $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + #echo "Connected Successfully!"; + + return $pdo; } catch(PDOException $e) { - die($e->getMessage()); + echo "Connection Failed: " . $e->getMessage(); } } diff --git a/chapter11/project1/databaseLogin.inc.php b/chapter11/project1/databaseLogin.inc.php index 32c54ae592e3b9896efa65c188b165152fe05b0e..611c74707269db53f9fdf2f7500600b66f4a6f55 100644 --- a/chapter11/project1/databaseLogin.inc.php +++ b/chapter11/project1/databaseLogin.inc.php @@ -1,6 +1,9 @@ <?php - $host = "localhost"; + #$host = "localhost"; + $host = "127.0.0.1"; $database = "bookcrm"; + $DBport = ""; + #$DBport = "3306"; $DBuser = "root"; $DBpassword = ""; ?> diff --git a/chapter11/project1/display-customers.php b/chapter11/project1/display-customers.php index 32f412942c722fd7a98408d03af28ca4477077c0..f818dc21c47bbf4fdb06593c99cfbfe43b0b0849 100644 --- a/chapter11/project1/display-customers.php +++ b/chapter11/project1/display-customers.php @@ -1,18 +1,32 @@ <?php + header('Content-Type: text/html; charset=ISO-8859-1'); + # Initialize database connection variables specific to this page. include "databaseLogin.inc.php"; include "databaseConnect.inc.php"; #ConnectViaMySQLi($host, $DBuser, $DBpassword, $database); - #ConnectViaPDO($host, $DBuser, $DBpassword, $database); + $pdo = ConnectViaPDO($host, $DBuser, $DBpassword, $database, $DBport); + + include "sqlStatements.inc.php"; + + # Initially load databases. + # Load Customers. + #$sql = SQLSelect("*", "Customers"); + $sql = SQLSelectOrder("*", "Customers", "LastName"); + $sqlCustomers = $pdo->query($sql); + + # Load Categories. + #$sql = SQLSelect("*", "Categories"); + $sql = SQLSelectOrder("*", "Categories", "CategoryName"); + $sqlCategories = $pdo->query($sql); - #include "sqlStatements.inc.php"; - #Initially load databases. - #$sqlCustomers = SQLSELECT("*", "Customers"); - #$sqlCategories = SQLSELECT("*", "Categories"); - #$sqlImprints = SQLSELECT("*", "Imprints"); + # Load Imprints. + #$sql = SQLSelect("*", "Imprints"); + $sql = SQLSelectOrder("*", "Imprints", "Imprint"); + $sqlImprints = $pdo->query($sql); ?> @@ -64,7 +78,21 @@ <th>University</th> <th>City</th> </tr> - + <?php + + if ($sqlCustomers) { + while ($row = $sqlCustomers->fetch()) { + echo "<tr>"; + echo "<td>" . $row['FirstName'] . " " . $row['LastName'] . "</td>"; + echo "<td>" . $row['Email'] . "</td>"; + echo "<td>" . $row['University'] . "</td>"; + echo "<td>" . $row['City'] . "</td>"; + echo "</tr>"; + } + } else { + echo "<p>No Customers to display.</p>"; + } + ?> </table> </div> </div> @@ -73,14 +101,30 @@ <div class="panel panel-info spaceabove"> <div class="panel-heading"><h4>Categories</h4></div> <ul class="nav nav-pills nav-stacked"> - + <?php + if ($sqlCategories) { + while ($row = $sqlCategories->fetch()) { + echo "<li><a href=#>" . $row['CategoryName'] . "</a></li>"; + } + } else { + echo "<li>No Categories to display.</li>"; + } + ?> </ul> </div> <div class="panel panel-info"> <div class="panel-heading"><h4>Imprints</h4></div> <ul class="nav nav-pills nav-stacked"> - + <?php + if ($sqlImprints) { + while ($row = $sqlImprints->fetch()) { + echo "<li><a href=#>" . $row['Imprint'] . "</a></li>"; + } + } else { + echo "<li>No Imprints to display.</li>"; + } + ?> </ul> </div> </div> <!-- end left navigation rail --> diff --git a/chapter11/project1/sqlStatements.inc.php b/chapter11/project1/sqlStatements.inc.php index 047da428bba5e80df8d84227f7420605b7fd84a7..262f33018d57a6946795160af7fae883851e8bd3 100644 --- a/chapter11/project1/sqlStatements.inc.php +++ b/chapter11/project1/sqlStatements.inc.php @@ -6,4 +6,14 @@ $query = "SELECT $attributes FROM $tables;"; return $query; } + + function SQLSelectOrder($attributes, $tables, $order) { + $query = "SELECT $attributes FROM $tables ORDER BY $order;"; + return $query; + } + + function SQLSelectOrderMatch($attributes, $tables, $order, $matcher, $matching) { + $query = "SELECT $attributes FROM $tables WHERE $matcher LIKE $matching ORDER BY $order;"; + return $query; + } ?> \ No newline at end of file