扩展数据库类

发布于 2024-09-24 21:47:37 字数 1710 浏览 5 评论 0原文

我有一个数据库类:

<?php

class dbConnect
{
    var $strHost = "";
    var $strDatabase = "";
    var $strUser = "";
    var $strPassword = "";

    var $intLinkID = 0;
    var $intQueryID = 0;
    var $arrRecord = array();
    var $intRow;

    var $intErrno = 0;
    var $strError = "";

    function dbConnect()
    {
        $this->Connect();
    }

    function dbHalt($strError)
    {
        $objError = new errorHandle();

        $objError->reportError("Database error: $strError", false);
        $this->intErrno = mysql_errno($this->intLinkID);
        $this->strError = mysql_error($this->intLinkID);

        $strError = sprintf("MySQL Error: %s (%s)", $this->intErrno, $this->strError);

        $objError->reportError($strError, true);
    }

    function Connect()
    {
        if (0 == $this->intLinkID) {

            $this->intLinkID = mysql_connect($this->strHost, $this->strUser, $this->strPassword);
            if (!mysql_select_db($this->strDatabase, $this->intLinkID)) {
                $this->dbHalt("cannot use database " . $this->strDatabase);
            }

        }

        if (!$this->intLinkID) {
            $this->dbHalt("Database connection failed");
        }


    }
}

class dbMain extends dbConnect
{
// Database connection settings.

// Dev server.
    var $strHost = 'db_host_here';
    var $strUser = 'db_User_here';
    var $strPassword = 'db_password_here';
    var $strDatabase = 'db_name_here';
}

调用 $objDB = new dbMain(); 时效果很好

,但我想要的是在另一个类(对于主 CMS)中动态设置数据库详细信息,例如:

$objDB = new dbMain($user, $pass, $dbname, $dbhost);

I have a database class:

<?php

class dbConnect
{
    var $strHost = "";
    var $strDatabase = "";
    var $strUser = "";
    var $strPassword = "";

    var $intLinkID = 0;
    var $intQueryID = 0;
    var $arrRecord = array();
    var $intRow;

    var $intErrno = 0;
    var $strError = "";

    function dbConnect()
    {
        $this->Connect();
    }

    function dbHalt($strError)
    {
        $objError = new errorHandle();

        $objError->reportError("Database error: $strError", false);
        $this->intErrno = mysql_errno($this->intLinkID);
        $this->strError = mysql_error($this->intLinkID);

        $strError = sprintf("MySQL Error: %s (%s)", $this->intErrno, $this->strError);

        $objError->reportError($strError, true);
    }

    function Connect()
    {
        if (0 == $this->intLinkID) {

            $this->intLinkID = mysql_connect($this->strHost, $this->strUser, $this->strPassword);
            if (!mysql_select_db($this->strDatabase, $this->intLinkID)) {
                $this->dbHalt("cannot use database " . $this->strDatabase);
            }

        }

        if (!$this->intLinkID) {
            $this->dbHalt("Database connection failed");
        }


    }
}

class dbMain extends dbConnect
{
// Database connection settings.

// Dev server.
    var $strHost = 'db_host_here';
    var $strUser = 'db_User_here';
    var $strPassword = 'db_password_here';
    var $strDatabase = 'db_name_here';
}

This works fine when calling $objDB = new dbMain();

But what I want is to dynamically set the database details in another class (for a master CMS) e.g.:

$objDB = new dbMain($user, $pass, $dbname, $dbhost);

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

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

发布评论

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

评论(1

陪我终i 2024-10-01 21:47:37

使用类构造函数。传入值。

class dbMain 
{
   function __construct($strHost='db_host_here',$strUser='db_User_here',$strPassword='db_password_here',$strDatabase='db_name_here'){
        $this->strHost = $strHost;
        $this->strUser = $strUser;
        $this->strPassword = $strPassword;
        $this->strDatabase = $strDatabase;
    }
}

Use a class constructor. Pass in values.

class dbMain 
{
   function __construct($strHost='db_host_here',$strUser='db_User_here',$strPassword='db_password_here',$strDatabase='db_name_here'){
        $this->strHost = $strHost;
        $this->strUser = $strUser;
        $this->strPassword = $strPassword;
        $this->strDatabase = $strDatabase;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文