A Waldrop
A Waldrop

Reputation: 11

Mysqli prepared statement fetch_assoc

I've tried over and over to figure out why this code isn't working, I have several rows in this table but when I try to execute this it is not returning the rows, everything else is working including the first prepared statement, I have tried using this code in other places and it works just fine, I can also bind the result to a variable and echo the info from the variable so it is there, I have no idea why it won't echo the rows, any advice appreciated!

<?php
include_once 'includes/functions.php';
include_once 'includes/create_pages.inc';

sec_session_start();

$proname = $_SESSION['programname'];
?>
<?php include('head.php'); ?>

<title>PECOC Add pages to </title>

<?php include('header-admin.php'); ?>

<?php if (login_check($mysqli) == true) : ?>

<h1>Create Pages for <?php echo htmlentities($proname); ?></h1>
        <?php
        if (!empty($error_msg)) {
            echo $error_msg;
        }

        $prep_stmt = "SELECT `program_id` FROM `programs` WHERE `program_name` = ?";
        $stmt = $mysqli->prepare($prep_stmt);
        if ($stmt){
            $stmt->bind_param('s', $proname);
            $stmt->execute();
            $stmt->bind_result($proid);
            while ($stmt->fetch()) [];
            $stmt->close();
        }
       $prep_stmt = "SELECT `page_name` FROM `pages` WHERE `fk_program_id`  = ? LIMIT 100";
    $stmt = $mysqli->query($prep_stmt);

    if ($stmt) {
        $stmt->bind_param('i', $proid);
        $stmt->execute();
        $stmt->store_result();
       while($row = $stmt->fetch_assoc()){
    echo $row['page_name'] . '<br />';
}

    } 





        ?>

        <ul>
            <li>Create a new program here.</li>

        </ul>





            <p>Return to <a href="index.php">login page</a></p>
        <?php else : ?>
            <p>
                <span class="error">You are not authorized to access this page.</span> Please <a href="index.php">login</a>.
            </p>
        <?php endif; ?>

<?php include('footer.php'); ?>

Upvotes: 1

Views: 283

Answers (1)

Dharman
Dharman

Reputation: 33238

fetch_assoc() is a method of mysqli_result not mysqli_stmt class. To use fetch_assoc() you need to get the result from statement first.

$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {

You could also loop on the result directly because it is traversable.

$stmt->execute();
foreach($stmt->get_result() as $row) {

Upvotes: 1

Related Questions