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 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)