Mysqli准备了语句fetch_assoc

时间:2015-09-11 17:22:10

标签: mysqli prepared-statement

我一遍又一遍地试图找出为什么这段代码不起作用,我在这个表中有几行但是当我尝试执行它时它不会返回行,其他一切都在工作,包括第一个准备好的语句,我有尝试在其他地方使用这个代码,它工作得很好,我也可以将结果绑定到一个变量并回显变量的信息,所以它在那里,我不知道为什么它不会回应行,任何建议赞赏!< / p>

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

1 个答案:

答案 0 :(得分:0)

fetch_assoc()mysqli_result而非mysqli_stmt类的方法。要使用fetch_assoc(),需要首先从语句获取结果。

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

您也可以直接循环搜索结果,因为它是可遍历的。

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