1. <legend id='9vhPg'><style id='9vhPg'><dir id='9vhPg'><q id='9vhPg'></q></dir></style></legend>

    <small id='9vhPg'></small><noframes id='9vhPg'>

  2. <i id='9vhPg'><tr id='9vhPg'><dt id='9vhPg'><q id='9vhPg'><span id='9vhPg'><b id='9vhPg'><form id='9vhPg'><ins id='9vhPg'></ins><ul id='9vhPg'></ul><sub id='9vhPg'></sub></form><legend id='9vhPg'></legend><bdo id='9vhPg'><pre id='9vhPg'><center id='9vhPg'></center></pre></bdo></b><th id='9vhPg'></th></span></q></dt></tr></i><div id='9vhPg'><tfoot id='9vhPg'></tfoot><dl id='9vhPg'><fieldset id='9vhPg'></fieldset></dl></div>
    • <bdo id='9vhPg'></bdo><ul id='9vhPg'></ul>
    1. <tfoot id='9vhPg'></tfoot>

      在准备好的语句上使用 fetch_assoc (php mysqli)

      时间:2023-07-30

        <legend id='lQH01'><style id='lQH01'><dir id='lQH01'><q id='lQH01'></q></dir></style></legend>
          <i id='lQH01'><tr id='lQH01'><dt id='lQH01'><q id='lQH01'><span id='lQH01'><b id='lQH01'><form id='lQH01'><ins id='lQH01'></ins><ul id='lQH01'></ul><sub id='lQH01'></sub></form><legend id='lQH01'></legend><bdo id='lQH01'><pre id='lQH01'><center id='lQH01'></center></pre></bdo></b><th id='lQH01'></th></span></q></dt></tr></i><div id='lQH01'><tfoot id='lQH01'></tfoot><dl id='lQH01'><fieldset id='lQH01'></fieldset></dl></div>
          <tfoot id='lQH01'></tfoot>
              <tbody id='lQH01'></tbody>
              <bdo id='lQH01'></bdo><ul id='lQH01'></ul>

              <small id='lQH01'></small><noframes id='lQH01'>

              • 本文介绍了在准备好的语句上使用 fetch_assoc (php mysqli)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我目前正在编写一个登录脚本,我得到了这个代码:

                I'm currently working on a login script, and I got this code:

                $selectUser = $db->prepare("SELECT `id`,`password`,`salt` FROM `users` WHERE `username`=?");
                $selectUser->bind_param('s', $username);
                $selectUser->execute();
                
                if ($selectUser->num_rows() < 0)
                    echo "no_user";
                else
                {
                    $user = $selectUser->fetch_assoc();
                    echo $user['id'];
                }
                

                这是我得到的错误:

                致命错误:未捕获的错误:调用未定义的方法mysqli_stmt::fetch_assoc()

                Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()

                我尝试了各种变体,例如:

                I tried all sorts of variations, like:

                $result = $selectUser->execute();
                $user = $result->fetch_assoc();
                

                还有更多……没有任何效果.

                and more... nothing worked.

                推荐答案

                那是因为 fetch_assoc 不是 mysqli_stmt 对象的一部分.fetch_assoc 属于 mysqli_result 类.可以使用mysqli_stmt::get_result先获取一个结果对象,然后调用fetch_assoc:

                That's because fetch_assoc is not part of a mysqli_stmt object. fetch_assoc belongs to the mysqli_result class. You can use mysqli_stmt::get_result to first get a result object and then call fetch_assoc:

                $selectUser = $db->prepare("SELECT `id`,`password`,`salt` FROM `users` WHERE `username`=?");
                $selectUser->bind_param('s', $username);
                $selectUser->execute();
                $result = $selectUser->get_result();
                $assoc = $result->fetch_assoc();
                

                或者,您可以使用 bind_result 将查询的列绑定到变量并使用 fetch() 代替:

                Alternatively, you can use bind_result to bind the query's columns to variables and use fetch() instead:

                $selectUser = $db->prepare("SELECT `id`,`password`,`salt` FROM `users` WHERE `username`=?");
                $selectUser->bind_param('s', $username);
                $selectUser->bind_result($id, $password, $salt);
                $selectUser->execute();
                while($selectUser->fetch())
                {
                    //$id, $password and $salt contain the values you're looking for
                }
                

                这篇关于在准备好的语句上使用 fetch_assoc (php mysqli)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:警告问题:期望参数 1 为 mysqli_result 下一篇:php 5.2.6 的 mysqli_stmt_get_result 替代方案

                相关文章

                最新文章

              • <small id='sK6Fa'></small><noframes id='sK6Fa'>

                  <bdo id='sK6Fa'></bdo><ul id='sK6Fa'></ul>
                <tfoot id='sK6Fa'></tfoot>

                    <legend id='sK6Fa'><style id='sK6Fa'><dir id='sK6Fa'><q id='sK6Fa'></q></dir></style></legend>
                    <i id='sK6Fa'><tr id='sK6Fa'><dt id='sK6Fa'><q id='sK6Fa'><span id='sK6Fa'><b id='sK6Fa'><form id='sK6Fa'><ins id='sK6Fa'></ins><ul id='sK6Fa'></ul><sub id='sK6Fa'></sub></form><legend id='sK6Fa'></legend><bdo id='sK6Fa'><pre id='sK6Fa'><center id='sK6Fa'></center></pre></bdo></b><th id='sK6Fa'></th></span></q></dt></tr></i><div id='sK6Fa'><tfoot id='sK6Fa'></tfoot><dl id='sK6Fa'><fieldset id='sK6Fa'></fieldset></dl></div>