首页 > 代码库 > 再说“使用CI操作oracle 10g的单表增删改查”

再说“使用CI操作oracle 10g的单表增删改查”

--Control中的UseOra.php

 

 

<?php

if (!defined(‘BASEPATH‘))
    exit(‘No direct script access allowed‘);

class UseOra extends CI_Controller
{

    public function index()
    {
        echo ‘欢迎使用测试CI使用Oracle 10g 的功能,您可以在地址栏中使用如下的函数参数:<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘selectData<br>‘;
        echo ‘selectDataWitchParm($deptno)<br>‘;
        echo ‘addData($deptno,$deptname,$deptloc)<br>‘;
        echo ‘delData($deptno)<br>‘;
        echo ‘updateData($detpno,$loc)<br>‘;
        echo ‘arSelectData()    使用ActiveRecord方式<br>‘;
        echo ‘arSelectDataWithParam($deptno)   使用ActiveRecord方式<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘注意:已经在autoload.php中自动加载了数据库通用类了!<br>‘;
        echo ‘========================================================================<br>‘;
        echo ‘使用的数据库平台是:‘ . $this->db->platform() . ‘<br>版本是:‘ . $this->db->version();
    }

    public function selectData()
    {
        $sql = ‘select deptno,dname,loc from dept order by deptno‘;
        $res = $this->db->query($sql);
        $depts = $res->result();
        foreach ($depts as $dept)
        {
            echo ‘编号:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名称:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function selectDataWitchParm($deptno)
    {
        $sql = ‘select deptno,dname,loc from dept where deptno=? order by deptno‘;
        $res = $this->db->query($sql, array($deptno));
        $depts = $res->result();
        echo ‘第一种方法:使用?占位符,参数作为数组<br>‘;
        foreach ($depts as $dept)
        {
            echo ‘编号:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名称:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }

        $sql = ‘select deptno,dname,loc from dept where deptno=‘ . $deptno;
        $res = $this->db->query($sql);
        $depts = $res->result();
        echo ‘第二种方法:构造字符串<br>‘;
        foreach ($depts as $dept)
        {
            echo ‘编号:‘ . $dept->DEPTNO . ‘<br>‘;
            echo ‘名称:‘ . $dept->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $dept->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function addData($deptno, $deptname, $deptloc)
    {
        $sql = ‘insert into dept(deptno,dname,loc) values(?,?,?)‘;
        $bool = $this->db->query($sql, array($deptno, $deptname, $deptloc));
        if ($bool)
        {
            echo ‘插入成功!‘;
        }
    }

    public function delData($deptno)
    {
        $sql = ‘delete from dept where deptno=?‘;
        $bool = $this->db->query($sql, array($deptno));
        if ($bool)
        {
            echo ‘删除数据成功!‘;
        }
    }

    public function updateData($detpno, $deptloc)
    {
        $sql = ‘update dept set loc=? where deptno =? ‘;
        $bool = $this->db->query($sql, array($deptloc, $detpno));
        if ($bool)
        {
            echo ‘更新数据成功!‘;
        }
    }

    public function arSelectData()
    {
        $this->load->model(‘MuseOra_AR‘);
        $rows = $this->MuseOra_AR->AR_selectData();
        foreach ($rows as $row)
        {
            echo ‘编号:‘ . $row->DEPTNO . ‘<br>‘;
            echo ‘名称:‘ . $row->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $row->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

    public function arSelectDataWithParam($deptno)
    {
        $this->load->model(‘MuseOra_AR‘);
        $rows = $this->MuseOra_AR->AR_selectDataWithParam($deptno);
        foreach ($rows as $row)
        {
            echo ‘编号:‘ . $row->DEPTNO . ‘<br>‘;
            echo ‘名称:‘ . $row->DNAME . ‘<br>‘;
            echo ‘地址:‘ . $row->LOC . ‘<br>‘;
            echo ‘----------------------<br>‘;
        }
    }

}

?>

 

 

--在model中的MuseOra_AR.php

<?php

class MuseOra_AR extends CI_Model
{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    function AR_selectData()
    {
        $this->db->order_by(‘deptno‘,‘ASC‘);//排序的方法
        $this->db->select(‘deptno,dname,loc‘);
        $res = $this->db->get(‘dept‘);
        return $res->result();
    }

    function AR_selectDataWithParam($deptno)
    {
        $this->db->where(‘deptno‘, $deptno);
        $this->db->select(‘deptno,dname,loc‘);
        $res = $this->db->get(‘dept‘);
        return $res->result();
    }

}
?>