Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用mysqli fetch assoc()获取mysqli结果时出错_Php_Mysqli - Fatal编程技术网

Php 使用mysqli fetch assoc()获取mysqli结果时出错

Php 使用mysqli fetch assoc()获取mysqli结果时出错,php,mysqli,Php,Mysqli,这是我用于连接数据库的类,它有一个查询方法用于循环查询结果,但它给了我以下错误: 致命错误:在C:\Apache24\htdocs\classes\DB.php的第30行调用未定义的方法mysqli::fetch_assoc() 我知道问题出在我的query()方法中,我尝试过使用非静态属性,但错误仍然存在 <?php class DB { private static $db_name = "data_db"; private static $d

这是我用于连接数据库的类,它有一个查询方法用于循环查询结果,但它给了我以下错误:

致命错误:
在C:\Apache24\htdocs\classes\DB.php的第30行调用未定义的方法mysqli::fetch_assoc()

我知道问题出在我的query()方法中,我尝试过使用非静态属性,但错误仍然存在

<?php

    class DB {
        private static $db_name = "data_db";
        private static $db_user = "root";
        private static $db_pass = "root";
        private static $db_host = "localhost";

        private static $row;
        private static $instance = null;

        public static function get_instance() {
            if(!isset(self::$instance))
                self::$instance = new self;

            return self::$instance;
        }

        //returns mysqli object.
        private function __construct() {
            $this->mysqli = new mysqli(self::$db_host, self::$db_user, self::$db_pass, self::$db_name);
        }

        public function __destruct() {
            $this->mysqli->close();
        }

        public function query($query) {
        if ($result = $this->mysqli->query($query)) {
            if($result->num_rows > 1) {
                $rows = array();            
                while ($item = $result->fetch_assoc()) {
                    $rows[] = $item;
                }
            } else {
                $rows = $result->fetch_assoc();
            }

                return $rows;
        }
    }

        /**
         * Private clone method to prevent cloning of the instance of the
         * *Singleton* instance.
         *
         * @return void
         */
        private function __clone() {}

        /**
         * Private unserialize method to prevent unserializing of the *Singleton*
         * instance.
         *
         * @return void
         */
        private function __wakeup() {}

    }
?>

MySQLi对象没有方法fetch\u assoc。您必须使用查询结果。例如:

public function query($query) {
    $result = $this->mysqli->query($query);

    $rows = array();
    while ($item = $result->fetch_assoc()) {
        $rows[] = $item;
    }

    return $items;
}

请是的,它是
mysqli\u result::fetch\u assoc
,而不是
mysqli::fetch\u assoc
。但现在它给了我这个错误:致命错误:允许的134217728字节内存大小耗尽(试图分配2176字节),在第30行的C:\Apache24\htdocs\classes\DB.php中
while($item=$result->fetch_assoc())
查询时返回的数据太多。请尝试修改查询或扩展内存限制。该表中只有5行。您在该表中存储了哪些数据?看看我的新查询方法。现在没事了@斯坦尼斯拉夫·德米杜克