简体   繁体   English

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php

[英]Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php

I have a problem with my code I'm getting this error message我的代码有问题 我收到此错误消息

Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\index.php on line 11 Fatal error: Uncaught Error: Call to a member function query() on null in C:\xampp\htdocs\shop\index.php:11 Stack trace: #0 {main} thrown in C:\xampp\htdocs\shop\ index.php 在第 11 行

index.php index.php

<?php

require_once __DIR__.'/function/database.php';

$sql = 'SELECT id, title, description, price FROM products';

$result = getDB()->query($sql);
require __DIR__.'/templates/main.php';

./function/database.php ./function/database.php

<?php 
function getDB() { 
    static $db;

    if ($db instanceof PDO){ 
        return $db; 
    }

    require_once CONFIG_DIR.'/database.php'; 

    $dsn = sprintf("myqsl:host=%s;dbname=%s;charset=%s",DB_HOST,DB_DATABASE,DB_CHARSET); 

    return $db; 
}

You forgot to create PDO connection:您忘记创建PDO连接:

<?php 
function getDB(){ 
    static $db; 
    if($db instanceof PDO){ 
        return $db; 
    } 
    require_once CONFIG_DIR.'/database.php'; 
    $dsn = sprintf("myqsl:host=%s;dbname=%s;charset=%s",DB_HOST,DB_DATABASE,DB_CHARSET); 
    $db = new PDO($dsn); // ← HERE
    return $db; 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 致命错误:在C:\\ xampp \\ htdocs \\ index.php中以布尔值调用成员函数bind_param() - Fatal error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\index.php 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ oophp \\ Home.php中调用成员函数connect()时为null - Fatal error: Uncaught Error: Call to a member function connect() on null in C:\xampp\htdocs\oophp\Home.php 致命错误:未捕获的错误:调用成员函数 prepare() 在 null [C:\\xampp\\htdocs\\twitter\\core\\classes\\user.php on line 19] - Fatal error: Uncaught Error: Call to a member function prepare() on null [C:\xampp\htdocs\twitter\core\classes\user.php on line 19] 致命错误:未捕获错误:调用 C:\\xampp\\htdocs\\phpmvc\\app\\models\\Mahasiswa_model.php:31 中未定义的函数 query() - Fatal error: Uncaught Error: Call to undefined function query() in C:\xampp\htdocs\phpmvc\app\models\Mahasiswa_model.php:31 致命错误:未捕获错误:在plugins / custom / index.php:131中对成员函数get_cart()的调用为null - Fatal error: Uncaught Error: Call to a member function get_cart() on null in plugins/custom/index.php:131 致命错误:在第 30 行调用 C:\\xampp\\htdocs\\magento\\test.php 中布尔值的成员函数 getChildren()? - Fatal error: Call to a member function getChildren() on boolean in C:\xampp\htdocs\magento\test.php on line 30? 致命错误:在第 16 行调用 C:\\xampp\\htdocs\\studysite\\test.php 中布尔值的成员函数 fetchAll() - Fatal error: Call to a member function fetchAll() on boolean in C:\xampp\htdocs\studysite\test.php on line 16 致命错误:在第42行上,在C:\\ xampp \\ htdocs \\ website \\ adduser.php中的布尔值上调用成员函数execute() - Fatal error: Call to a member function execute() on boolean in C:\xampp\htdocs\website\adduser.php on line 42 Codeigniter 4 composer update PHP Fatal error: Uncaught Error: Call to a member function run() on int in index.php - Codeigniter 4 composer update PHP Fatal error: Uncaught Error: Call to a member function run() on int in index.php 致命错误:未捕获错误:调用 function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 堆栈跟踪:#0 {main} SQLSERVER - Fatal error: Uncaught Error: Call to function sqlsrv_connect() in C:\xampp\htdocs\DBtest\sqlsrv.php:7 Stack trace: #0 {main} SQLSERVER
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM