php mysqli致命错误:在null上调用成员函数query()



im遇到此错误,我不知道如何修复。

'Fatal error: Call to a member function query() on null in C:xampphtdocscmslibdb.php on line 46'

代码:

class db {
public $mysqli;
function Connect() {
         $server = 'localhost';
        $dbusername = 'root';
        $database = 'cms';
        $dbpassword = 'lopaka';
        $this->mysqli = new mysqli($server, $dbusername, $dbpassword, $database);
        /* connectie bekijken */
        if (mysqli_connect_errno()) {
            printf("Connectie mislukt: %sn", mysqli_connect_error());
            exit();
        }
    }

function GetGallery() {
    $query = "SELECT * FROM menu";
    $action = $this->mysqli->query($query);
    while ($row = $action->fetch_assoc()) {
        $gallery .= '<a href="'.$siteurl.''.$row['url'].'" title="'.$row['alt'].'" data-gallery="" ><img src="'.$siteurl.''.$row['url'].'" width="75px" height="75px"></a>';    
    }
    return $gallery;
} 

}

谢谢!!!

编辑 - 现在工作,但是现在我有一些新错误:

注意:未定义的索引:c: xampp htdocs cmms lib lib db.php在第42行42

注意:未定义的索引:c: xampp htdocs cmms lib lib db.php in Line 42

注意:未定义的索引:c: xampp htdocs cmms lib lib db.php在第42行42

您应该将__construct()方法添加到类...这样添加...

public function __construct()
{           
    $this->Connect();
}

在这里,连接()将初始化,然后只有mySqli query()方法才能起作用。

     while ($row = mysqli_fetch_assoc($action)) {
            $gallery .= '<a href="'.$siteurl.''.$row['url'].'" title="'.$row['alt'].'" data-gallery="" ><img src="'.$siteurl.''.$row['url'].'" width="75px" height="75px"></a>';    
        }
 return $gallery;

最新更新