Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将我的sql中的查询转换为两个数据库中的Codeigniter_Php_Mysql_Codeigniter - Fatal编程技术网

Php 将我的sql中的查询转换为两个数据库中的Codeigniter

Php 将我的sql中的查询转换为两个数据库中的Codeigniter,php,mysql,codeigniter,Php,Mysql,Codeigniter,我真的需要帮助。我必须连接两个数据库,我已经尝试了我的查询,并在控制器中的本地模型中实现了,这段代码运行得非常好。这是我的sql查询: SELECT skapp.p.*, c1db_pegawai.e.eselon, c1db_pegawai.j.nama_jenis FROM skapp.ref_tunjangan_struktural p JOIN c1db_pegawai.r_eselon e on e.id = p.eselon_id

我真的需要帮助。我必须连接两个数据库,我已经尝试了我的查询,并在控制器中的本地模型中实现了,这段代码运行得非常好。这是我的sql查询:

SELECT skapp.p.*, c1db_pegawai.e.eselon, c1db_pegawai.j.nama_jenis FROM skapp.ref_tunjangan_struktural p
                JOIN c1db_pegawai.r_eselon e on e.id = p.eselon_id
                JOIN c1db_pegawai.r_jenis_pegawai j on j.id = p.jenis_pegawai_id
结果是:

我在Codeigniter中实现并将其添加到我的代码中:

$query = $this->db->query($sql);
在我看来,localhost也可以正常工作。这是可行的,因为我在本地有两个数据库,并在一个本地主机中查询所有这两个数据库。但在我的实时服务器中,我在不同的位置有两个数据库。我已经完成了连接it数据库配置,如Codeigniter文档中所述。我也在我的live server上尝试过这一点,这是我在模型中的代码:

$this->db->select('p.* as p'); 
        $this->another->select('e.eselon as e, j.nama_jenis as j');
        $this->db->from('ref_tunjangan_struktural');
        $this->another->join('r_eselon','r_eselon.id = ref_tunjangan_struktural.eselon_id');
        $this->another->join('r_jenis_pegawai','r_jenis_pegawai.id = ref_tunjangan_struktural.jenis_pegawai_id');
        $this->db->order_by("ref_tunjangan_struktural.id", "asc");
这是我的数据库连接控制器:

    $this->load->database('default',TRUE);  //first DB
    $this->another = $this->load->database('pegawai',TRUE); //second DB
但我的实时服务器上有一个错误结果。
$this->另一个
是我对第二个数据库的查询。但是仍然有一个错误。请任何人都可以帮助我修复我的实时服务器模型

这是我的错误吗

Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'as p FROM ref_tunjangan_struktural as p ORDER BY ref_tunjangan_struktural' at line 1 SELECT p.* as p` FROM ref_tunjangan_struktural as p ORDER BY ref_tunjangan_struktural.id ASC

如果这能解决您的问题:

$this->db->select('*'); 
$this->another->select('e.eselon as e, j.nama_jenis as j');
$this->db->from('ref_tunjangan_struktural'); 
$this->another->join('r_eselon','r_eselon.id = ref_tunjangan_struktural.eselon_id'); 
$this->another->join('r_jenis_pegawai','r_jenis_pegawai.id = ref_tunjangan_struktural.jenis_pegawai_id');
$this->db->order_by("ref_tunjangan_struktural.id", "asc");

不能这样做-不能混合使用2个QueryBuilder对象来创建一个查询

您唯一能做的就是使用第一个DB实例 连接到另一个数据库-但这有一个条件-您的用户 从您的第一个数据库必须访问第二个数据库-有 根本没有其他可能

在给定的条件下,以下各项应起作用

$query = $this->db
    ->select('skapp.p.*,c1db_pegawai.e.eselon, c1db_pegawai.j.nama_jenis')
    ->from('skapp.ref_tunjangan_struktural p')
    ->join('c1db_pegawai.r_eselon e', 'e.id = p.eselon_id')
    ->join('c1db_pegawai.r_jenis_pegawai j', 'j.id = p.jenis_pegawai_id')
    ->get();

print_r($query->result());

但仍有错误。
。。。发生了什么错误?数据库错误。错误号:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,了解使用near'as
p
FROM
ref\u tunjangan\u structural
as
p
ORDER BY
ref\u tunjangan\u structural在第1行选择
p
*as
p`FROM
ref\u tunjangan\u structural
as
p
ORDER BY
ref\u tunjangan\u strucktural
id
首先-显示错误类型,其次-我对您的结构一无所知-Codeigniter没有名为
另一个
的内置对象-换句话说,
$this->other
不是Codeigniter QueryBuilder
对象。对不起,这是我的控制器。我将$this另一个声明为第二个连接数据库:$this->load->database('default',TRUE)$this->other=$this->load->database('pegawai',TRUE);它可以工作,但不能回忆起“eselon”的值。就像我在上面mysql中的查询一样,它不能将“eselon”作为eselon返回。只需id$this->db->select('ref_tunjangan_strucktural.*);这个怎么样?还是一样。无法像my query aboce一样将eselon作为名称返回。仅以eselon_id和jenis_pegawai_id的形式查看没有像$this->other->from(“您的表名”)这样的从另一个数据库选择表方法?这与我上面的sql查询类似。我想要的是,如果有任何其他可能的方式在我的模型中逻辑地表达该条件,正如我已经说过的——你不能组合两个不同的QueryBuilder实例——这根本不可能。。。