当前位置:   article > 正文

thinkphp 操作远程oracle遇到的相关坑_thinkphp 链接oracle

thinkphp 链接oracle
坑一:没有内置oracle

        解决方法:

                1,下载think-oracle 扩展,资源很多,百度即可下载,分别放置于db下的connector 和 builder 文件夹

                2,安装oracle本地客户端,一搜一大把,核心注意不要下错版本

坑二:字符集错误导致有中文字符串的语句查询不到

        原因分析:因为远程oracle数据库采用的字符集是ZHS16GBK,我本地的程序是utf8,导致字符串与远程不匹配导致oracle无法查询

        解决方法:涉及到中文字符串的,先做转义,然后再代入查询,如下

  1. public function sup_waite_todu(){
  2. $text1 = iconv('utf-8', 'gbk','您有新的订单,请及时处理!【');
  3. $text2 = iconv('utf-8', 'gbk','】【采购员:');
  4. $text3 = iconv('utf-8', 'gbk','】【数量:');
  5. $text4 = iconv('utf-8', 'gbk','】');
  6. $effect = $this->db->query("select to_char(post_date,'yyyy-mm-dd hh24:mi:ss')||'".$text1."'||order_id||'".$text2."'||post_person||'".$text3."'|| (select sum(buy_num) from exp_order b where a.order_id = b.order_id and a.hospital_code = b.hospital_code)||'".$text4."' as info from exp_order_master a where a.sup_id = '1076' and a.hospital_code ='1001' and a.order_status='1' order by post_date desc");
  7. return $effect;
  8. }
坑三:一个项目牵扯多个数据库,程序一部分数据来自于远程oracle返回,需要配置多个

        解决方法:

                1,三方业务新建一个模型放于common下的model层,初始化直接连接远程,

                2,application下的config.php 增加数据库配置

                3,注意oracle的端口,需要根据实际情况变更

  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. use think\Config;
  6. /**
  7. * 供应商模型
  8. */
  9. header('Content-type: text/html; charset=ZHS16GBK');
  10. class Sup extends Model
  11. {
  12. protected function initialize()
  13. {
  14. $this->db = Db::connect(config('db_config1'));
  15. }
  16. //获取供应商信息
  17. public function sup_base_info($sup_id=''){
  18. $effect = $this->db->query("select b.hospital_code,c.f_fullname, a.expire_date,b.sup_name,b.sup_addres,b.sup_phone,b.sup_worker, a.f_createdate from xmadmin.xm_base_user a, exp_sup_dict b, xmadmin.xm_base_company c where a.f_encode = b.sup_id and b.hospital_code = c.f_companyid and a.f_encode='".$sup_id."'");
  19. return $effect;
  20. }
  21. }
  1. 'db_config1' => [
  2. // 数据库类型
  3. 'type' => 'oracle',
  4. // 数据库连接DSN配置
  5. 'dsn' => '',
  6. // 服务器地址
  7. 'hostname' => '',
  8. // 数据库名
  9. 'database' => '',
  10. // 数据库用户名
  11. 'username' => '',
  12. // 数据库密码
  13. 'password' => '',
  14. // 数据库连接端口
  15. 'hostport' => '',
  16. // 数据库连接参数
  17. 'params' => [],
  18. // 数据库编码默认采用utf8
  19. 'charset' => 'ZHS16GBK',
  20. // 数据库表前缀
  21. 'prefix' => '',
  22. ]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/184034
推荐阅读
相关标签
  

闽ICP备14008679号