PHP 类数据库连接范围问题

发布于 2024-07-06 22:22:56 字数 685 浏览 4 评论 0原文

对于我用 PHP 做的新项目,我创建了一个 SQLMethods 类来连接到数据库并执行查询。 今晚是我真正测试它的第一个晚上(我大约一周前写的,然后忘记了),并且发生了意外错误:当它调用我的 ExecuteQuery() 函数时,它不会使用我的数据库在构造函数中选择。

构造函数:

    public function SQLMethods() {
        $SQLConnection = mysql_connect($SQLDBAddress, $SQLUserName, $SQLPassword);

        if (!$SQLConnection) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($SQLDB, $SQLConnection);
    }

有问题的函数:

    public function ExecuteQuery($Query) {
        mysql_query($Query, $SQLConnection) or die('Could not perform query: ' . mysql_error());
    }

有人看到问题可能是什么吗? 构造函数完成后连接是否关闭?

For a new project that I'm doing in PHP I've created an SQLMethods class to connect to the database and perform queries. Tonight was the first night that I actually got to test it (I wrote it a week or so ago and forgot about it) and an unexpected error occured: When it was calling my ExecuteQuery() function, it wouldn't use the database I selected in the constructor.

The constructor:

    public function SQLMethods() {
        $SQLConnection = mysql_connect($SQLDBAddress, $SQLUserName, $SQLPassword);

        if (!$SQLConnection) {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db($SQLDB, $SQLConnection);
    }

The function in question:

    public function ExecuteQuery($Query) {
        mysql_query($Query, $SQLConnection) or die('Could not perform query: ' . mysql_error());
    }

Does anyone see what the issue might be? Does the connection close after the constructor completes?

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

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

发布评论

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

评论(3

浪菊怪哟 2024-07-13 22:22:56

您应该在您的类中声明 $SQLConnection,并且您应该将其称为

 $this->SQLConnection

$SQLConnection 而不仅仅是 $SQLConnection。

you should declare $SQLConnection in your class, and you should refer to it as

 $this->SQLConnection

and not simply $SQLConnection.

旧梦荧光笔 2024-07-13 22:22:56

ExecuteQuery 方法中不存在 $SQLConnection

您可以将其作为参数直接传递给 ExecuteQuery,也可以添加一个在构造函数中设置的 sqlConnection 类属性,并通过 $this->sqlConnection 进行访问 在你的类方法中。

$SQLConnection doesn't exist within the ExecuteQuery method.

You can either pass it directly as a parameter to ExecuteQuery, or add an sqlConnection class property that is set in the constructor and accessed as $this->sqlConnection inside your class methods.

成熟稳重的好男人 2024-07-13 22:22:56

尝试使用的变量 $SQLConnection ExecuteQuery() 是在另一个范围内创建的。 (SQLMethods 函数)。

当 PHP 脚本完成其工作或您自己关闭连接(如果在该脚本内建立连接)时,连接将关闭

您应该跳过 ExecuteQuery 中的 $SQLConnection 变量如 php.net 文档所述

如果未指定链接标识符,则假定为 mysql_connect() 打开的最后一个链接。

The variable $SQLConnection ExecuteQuery() is trying to use is created within another scope. (The SQLMethods function).

The connection closes when the PHP script has done its work or if you close it yourself (if the connection is made within that script)

You should skip the $SQLConnection variable within ExecuteQuery as stated by the php.net documentation

If the link identifier is not specified, the last link opened by mysql_connect() is assumed.

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