扩展数据库类
我有一个数据库类:
<?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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用类构造函数。传入值。
Use a class constructor. Pass in values.