dongxian2863 2017-01-01 01:40
浏览 60
已采纳

使用mysqli-> fetch_assoc() - 不能使用mysqli_result类型的对象作为数组

I recently found myself in this dilemma while programming a chunk of my application. I get the following error: Cannot use object of type mysqli_result as array
However, my application is turning the result into an array before trying to either return it or echo it. The code is inside a method definition. And yes, I have tried everything I could, including searching this site, including all the questions marked as duplicate. No awnsers.

Here is the code after I tried a solution:

public function lookupEmailByUsername($username)
        {
            $database = new mysqli(APP_DB_HOSTNAME, APP_DB_USERNAME, APP_DB_PASSWORD, APP_DB_DATABASE, APP_DB_PORT);
            $database->select_db("users"); // better safe than sorry

            $row = $database->query("SELECT * FROM `users` WHERE `username` = \"$username\";");

            if($row->num_rows == 1){
                $finalRow = $database->query("SELECT * FROM `users` WHERE `username` = \"$username\";");
                $finalRow->fetch_array();

                $finalresult = $finalRow['email'];

                return $finalresult; // Line where error occurs
            } else {

                $Logger = new logger();
                $Logger->logEvent("ERROR", "Could not find user $username for reverse lookup", true, 1);
                return null;
            }

        }

logger() is my logging class and it has no problems.

Code before I tried a solution:

public function lookupEmailByUsername($username)
        {
            $database = new mysqli(APP_DB_HOSTNAME, APP_DB_USERNAME, APP_DB_PASSWORD, APP_DB_DATABASE, APP_DB_PORT);
            $database->select_db("stories_users");

            if(!$database->query("SELECT * FROM `users` WHERE `username` = \"$username\";")){
                $logger = new logger();
                $logger->logEvent("ERROR", "Unable to execute search SQL for email lookup");
                return null;
            }
            $row = $database->query("SELECT * FROM `users` WHERE `username` = \"$username\";");


            if($row->num_rows == 1){
                $row->fetch_assoc();

                return $row['email'];
            } else {

                $Logger = new logger();
                $Logger->logEvent("ERROR", "Could not find user $username for reverse lookup", true, 1);
                return null;
            }

        }

But when I return $row['email'], the error occurs. Doing a var dump like this: var_dump($row->num_rows) returns 1, which is exactly what I am looking for. Why? Because if there is one row, it means there is one user. And my app doesn't allow duplicate users. This means the SQL is correct.

Why is return $row['email'] outputting that error when I tried to use fetch_array and fetch_assoc? There is one row as described above, and fetch_assoc should have turned it into an array.

OPS: I just found that row is not an array:

if(is_array($row)){
                    echo "row is array";
                    exit;
                }
                else {
                    echo "row is not array";
                    exit;
                }

But I used fetch_assoc. Sorry, I'm new to MySQLi's object oriented part.

  • 写回答

1条回答 默认 最新

  • dongwo5110 2017-01-01 01:43
    关注

    According to the documentation, mysqli_result::fetch_assoc() returns an associative array. It doesn not convert the mysqli_result instance into one. So, you'll want to store the value returned from fetch_assoc in a variable (you could use $row, which would require no further modification in your code).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程