Table of Contents

Web Development Lesson 7 - Databases

PHP Database Connection

Objective

In this activity you will use PHP to connect to the database and run a query.

Setup

<?php
    // begin HTML code
    include('header.php');
    include('menu.php');
?>

<?php
    // finish HTML code
    include('footer.php');
?>

Objects

$table->legs = 4;
$table->move($location);

PDO Connection

    // Set up database connection
    define ('DBCONNECT', "/home/dh_9m7wr9/pdo.php"); // database connection
    include_once DBCONNECT;
    $db  = 'techschoolwebdev';
    $dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
    try 
    { // connect
        $pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
    } catch (\PDOException $e) {
        throw new \PDOException ($e->getMessage(), (int)$e->getCode());
    }

PHP Queries

    $query = "SELECT heroes.character_name, hero_villain, first_appeared, power, movie
    FROM heroes
        LEFT JOIN appearances ON heroes.character_name=appearances.character_name";
    $args = array();
    $rslt  = $pdo->prepare($query);
    $rslt->execute($args);
    while ($row = $rslt->fetch()) {

    }
        error_log("MURRAY: query response row - " . print_r($row,1));
    <main>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Hero / Villain</th>
                    <th>First Appearance</th>
                    <th>Power</th>
                    <th>Movie</th>
                </tr>
            </thead>
            <tbody>
                
            </body>
        </table>
    </main>
    <main>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Hero / Villain</th>
                    <th>First Appearance</th>
                    <th>Power</th>
                    <th>Movie</th>
                </tr>
            </thead>
            <tbody>
<?php
            while ($row = $rslt->fetch()) {
                error_log("MURRAY: query response row - " . print_r($row,1));
    }
?>
            </tbody>
        </table>
    </main>
<?php
    while ($row = $rslt->fetch()) {
        error_log("MURRAY: query response row - " . print_r($row,1));
        echo "\t\t\t\t<tr>\n";
        echo "\t\t\t\t\t<td>$row['character_name']</td>\n";
        echo "\t\t\t\t\t<td>$row['hero_villain']</td>\n";
        echo "\t\t\t\t\t<td>$row['first_appeared']</td>\n";
        echo "\t\t\t\t\t<td>$row['power']</td>\n";
        echo "\t\t\t\t\t<td>$row['movie']</td>\n";
        echo "\t\t\t\t</tr>\n";
    }
?>
<?php
    while ($row = $rslt->fetch()) {
?>
                <tr>
                    <td><?php echo $row['character_name'];?></td>
                    <td><?php echo $row['hero_villain'];?></td>
                    <td><?php echo $row['first_appeared'];?></td>
                    <td><?php echo $row['power'];?></td>
                    <td><?php echo $row['movie'];?></td>
                </tr>
<?php
    }
?>

Repackaging Data

        $hero_data[$row['character_name']]['hero_villain']   = $row['hero_villain'];
        $hero_data[$row['character_name']]['first_appeared'] = $row['first_appeared'];
        $hero_data[$row['character_name']]['power']          = $row['power'];
        $hero_data[$row['character_name']]['movies']        .= $row['movie'] . "<br>";
    foreach($hero_data as $name => $details) {

    }
                <tr>
                    <td><?php echo $name;?></td>
                    <td><?php echo $details['hero_villain'];?></td>
                    <td><?php echo $details['first_appeared'];?></td>
                    <td><?php echo $details['power'];?></td>
                    <td><?php echo $details['movies'];?></td>
                </tr>
<?php
    // Set up database connection
	define ('DBCONNECT', "../../pdo.php"); // database connection
    include DBCONNECT;
    $db  = 'webdev';
    $dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
    try 
    { // connect
        $pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
    } catch (\PDOException $e) {
        throw new \PDOException ($e->getMessage(), (int)$e->getCode());
    }

    // begin HTML code
    include('header.php');
    include('menu.php');

    // extract data
    $query = "SELECT heroes.character_name, hero_villain, first_appeared, power, movie
    FROM heroes
        LEFT JOIN appearances ON heroes.character_name=appearances.character_name";
    $args = array();
    $rslt  = $pdo->prepare($query);
    $rslt->execute($args);
?>
    <main>
        <table>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Hero / Villain</th>
                    <th>First Appearance</th>
                    <th>Power</th>
                    <th>Movies</th>
                </tr>
            </thead>
            <tbody>
<?php
    while ($row = $rslt->fetch()) {
        $hero_data[$row['character_name']]['hero_villain']   = $row['hero_villain'];
        $hero_data[$row['character_name']]['first_appeared'] = $row['first_appeared'];
        $hero_data[$row['character_name']]['power']          = $row['power'];
        $hero_data[$row['character_name']]['movies']        .= $row['movie'] . "<br>";
    }
    foreach($hero_data as $name => $details) {
?>
                <tr>
                    <td><?php echo $name;?></td>
                    <td><?php echo $details['hero_villain'];?></td>
                    <td><?php echo $details['first_appeared'];?></td>
                    <td><?php echo $details['power'];?></td>
                    <td><?php echo $details['movies'];?></td>
                </tr>
<?php
    }
?>
            </body>
        </table>
    </main>
<?php
    // finish HTML code
    include('footer.php');

    $i = 0;
    while ($i < 10) {
        error_log("MURRAY: the value of i is $i");
        $i++;
    }

    $my_array = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');
    foreach($my_array as $name => $item) {
        error_log("MURRAY: $name is a $item");
    }
?>

Next: Exercises