简体   繁体   中英

How to fetch data for two prepared statements? (MySQLi)

How do I fetch results for two prepared statements using MySQLi?

I am trying to fetch data for two prepared statements. $stmt represents the contentBox div and $stmt2 represents the staffBox div.

$stmt works, fetches content boxes successfully without any errors. $stmt2 doesn't load any results at all for some reason.

Here's my code:

<?php

        if ($stmt = $conn->prepare("SELECT `id`, `name` FROM ranks ORDER BY `id` DESC"))
        {
            $stmt->execute();
            $stmt->bind_result($id, $rank);

            while($stmt->fetch())
            {
                echo '<div class="contentBox">
                <div class="contentHeader headerOwner">' . $rank . '</div>
                   <div class="contentInside" style="width:auto;margin:auto;text-align:center;margin-top: -2px;margin-left: 3px;margin-bottom: -4px;">';

                if ($stmt2 = $conn->prepare("SELECT `id`, `username`, `look`, `online` FROM `users` WHERE `rank` = ?"))
                {
                    $stmt2->bind_param('i', $id);
                    $stmt2->execute();
                    $stmt2->bind_result($id2, $username, $look, $online);

                    if ($stmt->num_rows <= 0)
                    {
                        echo '<div class="alert alert-danger">No records found.</div>';
                    }
                    else
                    {
                        while($stmt2->fetch())
                        {
                            echo '<div class="staffBox">
                            <div class="staffPicture" style="background-image: url(\'/media/useravatars/DefaultAvatar.png\');"></div>
                            <div class="staffName">
                            <div class="staffHabbo"><img src="https://www.habbo.nl/habbo-imaging/avatarimage?figure=ca-1801-1408.ch-3334-110-1408.ha-3331-110.hr-115-61.lg-3337-110.hd-180-1007.sh-3338-1408&direction=4&head_direction=4&action=wav&size=s" border="0"/></div>
                            <p style="margin: 5px 0px;font-weight: bold;"><a href="/user/' . $username . '" ng-click="progress()" class="username-black" tooltips="" tooltip-title="' . $username . '">' . $username . '</a></p>
                            <p style="margin: 5px 0px;">Owner</p>
                            <p style="margin: 5px 0px;"><i>United States</i></p>
                            </div></div>';
                        }
                    }

                    $stmt2->close();
                }

                echo '</div>
                </div>';
            }

            $stmt->close();
        }

    ?>

The left image is the result I get and the right image is the result I should be getting.

left image:

在此处输入图片说明

right image:

在此处输入图片说明

Thanks in advance.

You can't nest two prepared statements from a single connection unless you call store_result() after each query. The statement object is a pointer or a cursor to the underlying result set, calling store_result() tells the MySQL driver to fetch the entire result set into memory allowing you set a new pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM