在多个数据库连接(codeigniter php)帮助中生成查询

时间:2011-05-09 23:37:57

标签: php codeigniter

Assalamualaykum:)

我想在codeigniter中生成查询时遇到问题, 这是代码:

class Pengadilan_negeri extends CI_Controller {

    private function get_database_configuration($database_name) {
        $configuration = array(
            'hostname' => "DREAMBENDER\PENGADILANNEGERI",
            'username' => "sa",
            'password' => "12111993",
            'dbdriver' => "mssql"
        );

        $database_configuration = NULL;

        if ($database_name == "pidana") {
            $database_configuration = $configuration + array('database' => "dataPidana");
        } else if ($database_name == "perdata") {
            $database_configuration = $configuration + array('database' => "dataPerdata");
        }

        return $database_configuration;
    }

    public function biasa() {

        $DB1 = $this->load->database($this->get_database_configuration("pidana"), TRUE);
        $DB2 = $this->load->database($this->get_database_configuration("perdata"), TRUE);

        $DB1->query("SELECT * FROM dbo.DATA_REGISTER");

        foreach ($DB1->result() as $row) {
            print_r($row);
        }
    }
}

和这样的错误输出:

Fatal error: Call to undefined method CI_DB_mssql_driver::result() in E:\xampp\htdocs\pengadilan_negeri\application\controllers\register_perkara\perkara_pidana.php on line 33

我该怎么做才能使这段代码正常运行?

2 个答案:

答案 0 :(得分:1)

如果您使用MSSQL,请使用配置数据库中的端口,如下所示: '端口' => " 1433&#34 ;, 1433是默认端口

答案 1 :(得分:0)

您的biasa()函数中的

会更改您的查询。

public function biasa() {

    $DB1 = $this->load->database($this->get_database_configuration("pidana"), TRUE);
    $DB2 = $this->load->database($this->get_database_configuration("perdata"), TRUE);

    // execute the query and store it
    $query_db1 = $DB1->query("SELECT * FROM dbo.DATA_REGISTER");

    // call ->result() on the executed query to retrieve the results
    foreach ($query_db1->result() as $row) {
        print_r($row);
    }
}