04 Sessions Mysql 20 5.01 Mysqli_prepared_02

04 Sessions Mysql20 5.01 Mysqli_prepared_02

20 5.01 Mysqli_prepared_02
$_SERVER["SCRIPT_NAME"]
/php/assignments/04-sessions-mysql/20-5.01-mysqli_prepared_02.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 MySQL
    
$conn dbConnect('read');
    
// print_r(get_class_methods(get_class($conn)));
    // echo "\n\n";
    // var_dump($conn);
    // exit;

    
$sql 'SELECT image_id, filename, caption 
FROM php_a04_images
WHERE caption LIKE ?'
;

    
$searchterm '%' $_GET['search'] . '%';


    try {
        
$stmt $conn->stmt_init();
        
$stmt->prepare($sql);
        
$stmt->bind_param('s'$searchterm);
        
$stmt->execute();
        
$stmt->bind_result($image_id$filename$caption);
        
$stmt->store_result();
        
$numRows $stmt->num_rows;
    } 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>";
    }
    
?>

    <figure class="code">
        <pre class="language-sql"><code><?= $sql ?? ''?></code></pre>
    </figure>


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


        <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");
?>