Php 作为静态类的MySQLi

Php 作为静态类的MySQLi,php,mysql,oop,mysqli,Php,Mysql,Oop,Mysqli,我有以下代码,可以创建一个类,该类具有数据库对象的单个静态实例,以及行和列的两个静态函数 <?php class Database{ private static $instance; private function __construct() {} private function __clone(){} public static function call(){ if(!isset(self::$instance)){

我有以下代码,可以创建一个类,该类具有数据库对象的单个静态实例,以及行和列的两个静态函数

<?php class Database{

    private static $instance;

    private function __construct() {}
    private function __clone(){}

    public static function call(){
        if(!isset(self::$instance)){  
            self::$instance = new MySQLi("localhost", "foo", "bar", "fizz");  
            if(self::$instance->connect_error){  
                throw new Exception('Error MySQL: ' . self::$instance->connect_error);  
            }  
        } 
        return self::$instance;
    }

    public static function getRow($id, $db){
        $query = "SELECT * FROM ".$db." WHERE id=".$id;
        $result = self::call()->query($query);
        return $result->fetch_assoc();
    }
} ?>
我得到以下错误:

致命错误:调用成员函数 在中的非对象上获取\u assoc() database.php 第27行


我一次又一次地检查,似乎一切正常,也许我在call()中出错了

如果您的SQL语法有错误,就会发生这种情况——您是否检查了它并确保没有错误

我会进行错误检查,比如

if( self::$instance->error ) {
    // Handle error
}

如果您的SQL语法有错误,就会发生这种情况——您是否检查了它并确保没有错误

我会进行错误检查,比如

if( self::$instance->error ) {
    // Handle error
}

您正在将名为$db的变量传递给getRow(),可能应该是一个表?

您正在将名为$db的变量传递给getRow(),可能应该是一个表?

query()
如果SQL查询失败,将返回
false
。您可以添加以下内容来处理此错误:

$result = self::call()->query($query)
if($result === false)
    throw new Exception(call()->error); // throw exception, use MySQLi error as message

return $result->fetch_assoc();
如果SQL查询失败,
query()
将返回
false
。您可以添加以下内容来处理此错误:

$result = self::call()->query($query)
if($result === false)
    throw new Exception(call()->error); // throw exception, use MySQLi error as message

return $result->fetch_assoc();

我已经尝试了这个代码,它的作品,你一定有一个错误的查询。可能导致此错误的原因是:

  • 检查传递给
    数据库::getRow()
    的内容。您的id中可能有一些非数字字符

  • 检查列名
    'id'
    是否存在,这听起来很有趣,但它发生在我身上一百万次,我是在查询列
    'id'
    而不是
    'product\u id'
    等等

  • 检查表
    $db
    是否存在

  • 确保不要意外切换功能参数<代码>数据库::getRow(24,'人')而不是
    数据库::getRow('people',24)


  • 我想不出任何其他可能导致此错误的原因。

    我已经尝试了此代码,它可以正常工作,您的查询中一定有错误。可能导致此错误的原因是:

    public static function getRow($id,$table)
    {
        $query = "SELECT * FROM ".$table." WHERE id=".$id." LIMIT 1";
        $result = $this->instance->query($query);
        return $result->fetch_assoc();
    }
    
  • 检查传递给
    数据库::getRow()
    的内容。您的id中可能有一些非数字字符

  • 检查列名
    'id'
    是否存在,这听起来很有趣,但它发生在我身上一百万次,我是在查询列
    'id'
    而不是
    'product\u id'
    等等

  • 检查表
    $db
    是否存在

  • 确保不要意外切换功能参数<代码>数据库::getRow(24,'人')而不是
    数据库::getRow('people',24)

  • 我想不出任何其他可能导致此错误的原因。

    这行

    public static function getRow($id,$table)
    {
        $query = "SELECT * FROM ".$table." WHERE id=".$id." LIMIT 1";
        $result = $this->instance->query($query);
        return $result->fetch_assoc();
    }
    
    if(!isset(self::$instance)){  
    
    有时会有奇怪的行为。根据空值检查isset()可能会失败。试试这个:

    private static $instance = null; // it defaults to null but hey, rather be on the sure side
    // foo
    if (self::$instance === null) {
    // mysqli creation foo
    
    这条线

    if(!isset(self::$instance)){  
    
    有时会有奇怪的行为。根据空值检查isset()可能会失败。试试这个:

    private static $instance = null; // it defaults to null but hey, rather be on the sure side
    // foo
    if (self::$instance === null) {
    // mysqli creation foo
    

    这不是一个好主意,它基本上是在后门引入一个单身汉。@GordonM重新提出一个老问题的方式,但你是对的。我现在比这好多了。首先,我不再写PHP了,不是我!这已经接近PHP问题列表的顶部,我没有注意到上面的日期。这不是一个好主意,它基本上是在后门引入一个singleton。@GordonM是恢复一个旧问题的方法,但你是对的。我现在比这好多了。首先,我不再写PHP了,不是我!这已经接近PHP问题列表的顶部,我没有注意到上面的日期。