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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化