简体   繁体   English

PHP类数据库连接

[英]PHP Class DB Connections

I have a class which I'll like to include couple db routines instead of inline. 我有一个我想包含几个db例程而不是内联的类。

class Classica{

public $dbhost;
public $dbname;
public $dbuser;
public $dbpass;


__function construct(){
    //hook connectdb() upon contruct.
}

#connect to database
    public function connectdb($dbhost,$dbname,$dbuser,$dbpass){        

        $link = mysql_connect($dbhost,$dbuser,$dbpass);
        if (!$link) {
          die('Could not connect: ' . mysql_error());
        }else {
            //echo 'Connected Successfully to Database<br>';
        }
        @mysql_select_db($dbname) or die( "Unable to select database");
    } 

    #read database
    function readdb(){        
    }    
    #update database
    private function updatedb(){        
    }
    #close database connection
    function closedb(){
        mysql_close();    
    }

}

config.php config.php

$dbhost = 'localhost';
$dbname = 'dem';
$dbuser = 'root';
$dbpass = '';

index.php index.php

<?php
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
session_start();
include('config.php');
include('classica.php');
$obj = new classica();

?>

The database connection details I will like to maintain on a separate config file which is included on document load. 我想在一个单独的配置文件中维护数据库连接的详细信息,该文件包含在文档加载中。

The problems I'm having: 我遇到的问题:

Retrieving database config values inside the class. 检索类内部的数据库配置值。 Trying to attach a separate database class to separate from parent. 尝试附加一个单独的数据库类以与父类分开。

Any suggestions of approach and/or solution. 有关方法和/或解决方案的任何建议。

You can set the config variables in the global scope 您可以在全局范围内设置配置变量

public function __construct()
{
    global $dbhost, $dbname, etc... ;
    $this->connectdb($dbhost, $dbname, etc...);

Or use constants , its scope is global: 或使用常量 ,其范围是全局的:

config.php config.php

define('MYAPP_DBHOST', 'localhost');
define('MYAPP_DBNAME', 'dem');

and in your class: 在你的课上:

public function __construct()
{
    $this->connectdb(MYAPP_DBHOST, MYAPP_DBNAME, etc...);

The simplest method would be do include('config.php') inside the connectdb function (or the function calling it). 最简单的方法是在connectdb函数(或调用它的函数include('config.php')执行include('config.php') )。 When you include a PHP file (remember to include <?php open tag!), the variables will become declared within the scope of the function doing the inclusion. 当您include一个PHP文件(切记要包含<?php open标签!)时,这些变量将在进行包含的函数范围内声明。 So for example, __construct() (note that you have a typo) may be: 因此,例如__construct()(请注意,您有错字)可能是:

function __construct() {
    include('config.php');
    $this->connectdb($dbhost,$dbname,$dbuser,$dbpass);
}

Are you asking how to read from a config file? 您是否在询问如何从配置文件读取? Try 'parse_ini_file'. 尝试“ parse_ini_file”。 http://php.net/manual/en/function.parse-ini-file.php http://php.net/manual/zh/function.parse-ini-file.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM