Table of Contents

Desarrollo Web Lección 7 - Bases de Datos

Conexión de Base de Datos PHP

Objetivo

En esta actividad utilizará PHP para conectarse a la base de datos y ejecutar una consulta.

Preparación

<?php
    // comienza el código HTML
    include('cabecera.php');
    include('menu.php');
?>

<?php
    // terminar código HTML
    include('pie.php');
?>

Objetos

$mesa->patas = 4;
$mesa->mover($ubicación);

Conexión PDO

    // Configurar la conexión a la base de datos
    define ('DBCONNECT', "/home/dh_9m7wr9/pdo.php"); // conexión a la base de datos
    include_once DBCONNECT;
    $db = 'techschoolwebdev';
    $dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
    try
    { // conectar
        $pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
    } catch (\PDOException $e) {
        throw new \PDOException ($e->getMessage(), (int)$e->getCode());
    }

Consultas PHP

    $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
    }
?>

Datos de Reempaquetado

        $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>

Codigo Final

Next: Exercises