douwei8911 2015-09-11 17:22
浏览 63

Mysqli准备了语句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'); ?>
  • 写回答

1条回答 默认 最新

  • duankang5882 2019-07-28 18:51
    关注

    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) {
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型