04 Sessions Mysql 20 5.01 Pdo_prepared_03

04 Sessions Mysql20 5.01 Pdo_prepared_03

20 5.01 Pdo_prepared_03
$_SERVER["SCRIPT_NAME"]
/php/assignments/04-sessions-mysql/20-5.01-pdo_prepared_03.php

print_r($_GET)
Array
(
)


print_r($_POST)
Array
(
)


print_r($_SESSION)
Array
(
)


print_r($_FILES)
Array
(
)


<?php
if (isset($_GET['go'])) {
    // Database Connection
    require_once('../../includes/connection.php');

    // Utility Functions
    require_once '../../includes/utility_funcs.php';

    // Connect to Database Using PDO
    $conn = dbConnect('read', 'pdo');

    $sql = 'SELECT image_id, filename, caption 
FROM php_a04_images
WHERE caption LIKE :search';


    try {
        $stmt = $conn->prepare($sql);
        $stmt->bindValue(':search', '%' . $_GET['search'] . '%');
        $stmt->execute();
        $error = $stmt->errorInfo()[2];

        if (!$error) {
            $stmt->bindColumn('image_id', $image_id);
            $stmt->bindColumn('filename', $filename);
            $stmt->bindColumn('caption', $caption);
            $numRows = $stmt->rowCount();
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
} // END if (isset($_GET['go']))



$tools = true;
include("../../includes/header.php");
?>

<main>
    <h2><?php echo $folder_name; ?><span><?php echo $file_name; ?></span></h2>

    <form method="get">
        <fieldset>
            <legend><?php echo $file_name; ?></legend>
            <ol>
                <li>
                    <label for="search"></label>
                    <input type="text" name="search" id="search">
                </li>
                <li>
                    <input type="submit" name="go" id="go" value="Search">
                </li>
            </ol>
        </fieldset>
    </form>
    <?php
    if (isset($error)) {
        echo "<p class=\"error\">$error</p>";
    }
    ?>

    <?php if (isset($stmt)) { ?>
        <figure class="code">
            <pre class="language-sql"><code><?= $stmt->debugDumpParams() ?></code></pre>
        </figure>
    <?php } ?>

    <?php if (isset($numRows)) { ?>
        <p class="info">Number of results for <b><?= safe($_GET['search']) ?></b>: <?= $numRows; ?></p>

        <?php if ($numRows) { ?>
            <table id="output-sql">
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">filename</th>
                    <th scope="col">caption</th>
                </tr>
                <?php while ($stmt->fetch()) { ?>
                    <tr>
                        <td><?= $image_id; ?></td>
                        <td><?= safe($filename); ?></td>
                        <td><?= safe($caption); ?></td>
                    </tr>
                <?php } ?>
            </table>
    <?php }
    } ?>
</main>
<?php
# Close the prepared statement
//$stmt->close();

# The side-bar section of the layout use custom path to load from a different folder.
include("../../includes/sidebar.php");

# The footer section of the layout.
include("../../includes/footer.php");
?>