PHP - 类的静态函数内的 mysql_query()

发布于 2024-12-27 21:04:27 字数 2186 浏览 3 评论 0原文

情况

我在数据库中有一个表,其中包含工作类型,基本上由标签和价格定义。我正在尝试执行一个简单的 SELECT * FROM jobtype ,但我似乎无法让它工作,尽管我在其余代码中一遍又一遍地使用相同的块。这里的主要区别在于它是一个尝试执行该函数的单例。

问题是,一旦我取消注释行 $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error()); 页面就会在代码中的这个特定点停止加载。

下面的代码

是我的函数 getJobTypes() 的代码:

require_once('Connections/vtconnection.php');
class JobTypes extends Singleton{
    static private $job_types;

    public static function getJobTypes(){
        if (self::$job_types == null){
            echo 'DEBUG: For now, $job_types is NULL.'."\n";
            mysql_select_db($database_vtconnection, $vtconnection);
            $query_job_types = 'SELECT * FROM jobtype';
            $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error());

            while ($rs_row = mysql_fetch_assoc($rs_job_types)){
                // let the job type identifier in the db be its index in our array
                self::$job_types[$rs_row['id']]['label'] = $rs_row['label']; // job type label
                self::$job_types[$rs_row['id']]['price'] = $rs_row['price']; // job type price
            }
            if (self::$job_types != null) echo 'DEBUG: $job_types has been populated.'."\n";
        }
        return self::$job_types;
    }
}

我这样调用:

$jt = JobTypes::getJobTypes();

这是我的单例模式:

class Singleton{
    private static $instances = array();

    final private function __construct(){
    }

    final public function __clone(){
        trigger_error('You don\'t clone a singleton!', E_USER_ERROR);
    }

    final public static function getInstance(){
        $c = get_called_class();
        if(!isset(self::$instances[$c])){
            self::$instances[$c] = new $c;
        }
        return self::$instances[$c];
    }
}

我已经在脑海中解决了这个问题,评论了 getJobtypes 中的所有内容() 函数并逐步取消注释。我发现问题确实发生在 mysql_query() 行,但似乎无法找出原因。我的代码中有明显错误吗?


已解决

正如这里所建议的,我在静态函数开始时使用了 global $vtconnection,$database_vtconnection; ,一切都很顺利。这不是最佳解决方案,但它指出了我现在将尝试解决的范围问题。

我还摆脱了单例模式。

The Situation

I have a table in a DB that contains job types, basically defined by a label and a price. I'm trying to do a simple SELECT * FROM jobtype but I can't seem to get it to work, although I've use the same block over and over again in the rest of the code. The main difference here, is that it is a singleton trying to execute the function.

The problem is that as soon as I uncomment the line $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error()); the page will stop loading at this particular point in the code.

The Code

Following is the code of my function getJobTypes():

require_once('Connections/vtconnection.php');
class JobTypes extends Singleton{
    static private $job_types;

    public static function getJobTypes(){
        if (self::$job_types == null){
            echo 'DEBUG: For now, $job_types is NULL.'."\n";
            mysql_select_db($database_vtconnection, $vtconnection);
            $query_job_types = 'SELECT * FROM jobtype';
            $rs_job_types = mysql_query($query_job_types, $vtconnection) or die(mysql_error());

            while ($rs_row = mysql_fetch_assoc($rs_job_types)){
                // let the job type identifier in the db be its index in our array
                self::$job_types[$rs_row['id']]['label'] = $rs_row['label']; // job type label
                self::$job_types[$rs_row['id']]['price'] = $rs_row['price']; // job type price
            }
            if (self::$job_types != null) echo 'DEBUG: $job_types has been populated.'."\n";
        }
        return self::$job_types;
    }
}

Which I am calling like so:

$jt = JobTypes::getJobTypes();

Here is my singleton pattern:

class Singleton{
    private static $instances = array();

    final private function __construct(){
    }

    final public function __clone(){
        trigger_error('You don\'t clone a singleton!', E_USER_ERROR);
    }

    final public static function getInstance(){
        $c = get_called_class();
        if(!isset(self::$instances[$c])){
            self::$instances[$c] = new $c;
        }
        return self::$instances[$c];
    }
}

I have turned the problem in my head, commented everything inside the getJobtypes() function and uncommented step by step. I found out the problem does happen with the mysql_query() line, just can't seem to work out why. Is something obviously wrong in my code?


Solved

As suggested here, I used global $vtconnection,$database_vtconnection; at the start of my static function and all went smooth. It is not an optimal solution but it pointed out the scope issue which I will now try to resolve.

I also got rid of the singleton pattern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2025-01-03 21:04:27

最明显的事情是 $database_vtconnection $vtconnection 在您的 getJobTypes 函数中没有定义。如果它们是类的一部分,则需要 $this (对象)或 self (静态)引用。更有可能的是,您正在尝试使用全局变量,在这种情况下,您必须将它们拉入函数的范围。

mysql_select_db 可以自动连接(如果$vtconnection 未提供),但前提是它知道如何 - 存在先前的连接(或可能带有 db/host/user/pass 的 INI 配置)。

要将它们拉入作用域,您需要在函数的开头添加以下行:

global $vtconnection,$database_vtconnection;`

... 或使用 $GLOBALS 超全局数组:

mysql_select_db($GLOBALS["database_vtconnection"],$GLOBALS["vtconnection"]);

据记录,使用全局变量并不是一个特别好的解决方案。

Well the most obvious thing is $database_vtconnection $vtconnection are defined nowhere in your getJobTypes function. If they're part of the class, you need $this (object) or self (static) references. More likely it looks like you're trying to use global variables, in which case you have to pull them into the scope of the function.

mysql_select_db can auto-connect (if $vtconnection isn't supplied) but only if it knows how - there's a previous connection (or possible INI config with db/host/user/pass).

To pull them into scope you need to add the line at the beginning of the function:

global $vtconnection,$database_vtconnection;`

... Or use the $GLOBALS superglobal array:

mysql_select_db($GLOBALS["database_vtconnection"],$GLOBALS["vtconnection"]);

For the record using globals isn't a particularly good solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文